Skip to content

Commit

Permalink
fix: add micromark parser to markdownlint config
Browse files Browse the repository at this point in the history
In markdownlint 0.36.0, the support for micromark returned. Adding it in the config
object fixes the error described in
[this issue](#7)

The new micromark version changes some previously assumed parser behaviors. It causes
some errors related to nested intentation in HTML snippets. Since currently there is no
need to lint HTML, the tokens related to it are now ignored.

Introduces a new test verification to prevent an error where  tokens cause
other text tokens to appear to be indented.
  • Loading branch information
MatheusBaldi committed Nov 29, 2024
1 parent d2a86c9 commit d4bc349
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 26 deletions.
162 changes: 143 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@silvermine/eslint-config": "3.1.0-beta.0",
"@silvermine/standardization": "2.2.3",
"chai": "4.3.7",
"markdownlint": "0.29.0",
"markdownlint": "0.36.1",
"mocha": "10.2.0"
},
"dependencies": {
Expand Down
29 changes: 23 additions & 6 deletions src/indent-alignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ function traverse(tokens, nodeTypes, iterateeFn) {
iterateeFn(token);
}

if (token.children) {
// Avoid linting html tags (`htmlFlow`) because it would create too many edge cases
// due to the way micromark parses HTML tokens. Currently any tokens inside a
// `paragraph` token are considered siblings, so it's too hard to define which
// elements should be aligned or indented.
if (token.children && token.type !== 'htmlFlow') {
traverse(token.children, nodeTypes, iterateeFn);
}
});
Expand All @@ -22,6 +26,14 @@ function iterate(tokens, nodeTypes, iterateeFn) {
skipTo = undefined;
}

// `htmlText` tokens cause other text tokens to appear to be indented, so we avoid
// linting lines that include it. Note: Micromark parser currently has a weird
// behavior where multiline HTML tags are incorrectly classified as paragraph
// tokens.
if (token.type.includes('htmlText')) {
skipTo = 'lineEnding';
}

const isSkippingTokens = skipTo !== undefined,
visitRequested = !nodeTypes || nodeTypes.includes(token.type);

Expand All @@ -33,9 +45,14 @@ function iterate(tokens, nodeTypes, iterateeFn) {
});
}

function findFirstTokenOfType(tokens, nodeTypes) {
function findFirstTokenOfTypeToVisit(tokens, nodeTypes) {
let skipLine;

return (tokens || []).find((token) => {
return nodeTypes.includes(token.type);
if (token.type === 'htmlText') {
skipLine = token.endLine;
}
return nodeTypes.includes(token.type) && token.startLine !== skipLine;
});
}

Expand All @@ -57,6 +74,7 @@ module.exports = {
description: 'Indent alignment of list items, wrapped lines, and blocks',
information: new URL('https://github.com/silvermine/markdownlint-rule-indent-alignment'),
tags: [ 'bullet', 'ul', 'il', 'indentation', 'paragraph' ],
parser: 'micromark',

'function': function listIndentation(params, onError) {
// Ensure top-level blocks and paragraphs are not indented
Expand Down Expand Up @@ -107,10 +125,9 @@ module.exports = {
'link',
'literalAutolink',
'strong',
'htmlText',
];

const firstToken = findFirstTokenOfType(token.children, inlineTextLikeTokens);
const firstToken = findFirstTokenOfTypeToVisit(token.children, inlineTextLikeTokens);

if (!firstToken) {
return;
Expand Down Expand Up @@ -142,7 +159,7 @@ module.exports = {
traverse(params.parsers.micromark.tokens, [ 'blockQuote', 'listOrdered', 'listUnordered' ], (token) => {
let iterateOverChildTokens = [ 'codeFenced', 'content', 'listItemPrefix', 'blockQuote', 'listOrdered', 'listUnordered' ];

const firstToken = findFirstTokenOfType(token.children, [ 'content' ]);
const firstToken = findFirstTokenOfTypeToVisit(token.children, [ 'content' ]);

if (!firstToken) {
return;
Expand Down
7 changes: 7 additions & 0 deletions tests/indent-alignment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,13 @@ describe('Indent Alignment', () => {
' Lorem ipsum dolor sit amet',
'</div>',
]);

await testValidExample([
'<a',
'href="#">',
' link text',
'</a>',
]);
});

it('reports errors for incorrect indention of item under long ol list item prefixes', async () => {
Expand Down

0 comments on commit d4bc349

Please sign in to comment.