Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add micromark parser to markdownlint config #11

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 33 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 blocks because the commonmark spec does not define the proper
// parent/child structure that would be needed to validate blocks of html text. For
// example, a div that's inside another div is still considered a sibling by the
// commonmark standard. See also: https://spec.commonmark.org/0.31.2/#html-blocks
if (token.children && token.type !== 'htmlFlow') {
traverse(token.children, nodeTypes, iterateeFn);
}
});
Expand All @@ -22,6 +26,24 @@ function iterate(tokens, nodeTypes, iterateeFn) {
skipTo = undefined;
}

// If an HTML tag spans multiple lines, and is not considered a "starting condition"
// it will be parsed as part of a paragraph instead of an HTML block. For example:
// ```md
// <a
// href="">this text is inside a paragraph.</a>
//
// <a href="">this text is inside a block of HTML.</a>
// ```
// See "starting condition" for more details:
// https://spec.commonmark.org/0.31.2/#html-blocks
//
// Since we want to avoid linting any HTML content, we skip any tokens that appear
// on the same line as `htmlText`. Note that if we did not do this, any text before
// or after `htmlText` could be inaccurately flagged by this rule.
if (token.type === 'htmlText') {
skipTo = 'lineEnding';
}

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

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

function findFirstTokenOfType(tokens, nodeTypes) {
function findFirstNonHTMLTokenOfType(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 +84,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 +135,9 @@ module.exports = {
'link',
'literalAutolink',
'strong',
'htmlText',
];

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

if (!firstToken) {
return;
Expand Down Expand Up @@ -142,7 +169,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 = findFirstNonHTMLTokenOfType(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