Repository navigation
Disable markdoc tag in link href in tokenizer - #624
rayc-stripe merged 7 commits into
Conversation
806c32b to
74dd880
Compare
yue-stripe
left a comment
There was a problem hiding this comment.
I would recommend reusing markdown-it's markdown AST instead of trying to parse markdown links ourselves
| for (token of state.tokens) { | ||
| if (token.type !== 'inline' || typeof token.content !== 'string') continue; | ||
|
|
||
| const matches = token.content.match(MARKDOWN_LINK_DESTINATION) || []; |
There was a problem hiding this comment.
Using raw token content would incorrectly flag content that isn't rendered as a link e.g.
`[Link]({% tag %})`
which doesn't render a Link but an inline code component with the content.
Additionally the regex won't work with URLs with parentheses like https://en.wikipedia.org/wiki/Exam_(disambiguation){% tag %} (regex)
We should walk the AST produced by markdown-it instead of trying to parse markdown links ourselves. Something like
function getAttr(token: Token, name: string) {
return token.attrs?.find(([key]) => key === name)?.[1];
}
const href = getAttr(token, 'href');
if (href && containsMarkdocTag(href)) {
pushHrefError(token);
}
There was a problem hiding this comment.
href is only parsed and stored when the link is valid. I fix the failed test cases by tracing the start position from a the start tag (%{) and see if the content contains the https:// instead.
|
|
||
| export const OPEN = '{%'; | ||
| export const CLOSE = '%}'; | ||
| export const TAG_PATTERN = /\{%\s*[\s\S]*?%\}/; |
There was a problem hiding this comment.
This regex duplicates the markdoc tag scanning logic defined later in this file. Can we make a helper function containsMarkdocTag that checks for OPEN + findTagEnd(), something like
export function containsMarkdocTag(content: string): boolean {
for (let pos = 0; pos < content.length; pos++) {
if (!content.startsWith(OPEN, pos)) continue;
const end = findTagEnd(content, pos);
if (end != null) return true;
// Mirror parseTags(): ignore a dangling `{%` and keep scanning.
pos += OPEN.length - 1;
}
return false;
}
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Markdoc doesn't have any validation to fail the use of markdoc variables or tags (i.e.
{% %}format) in link href.In this PR, we capture the link format in the tokenizer, and push an error when the tag format is found in the href.
Another option is to run the validation in link schema, but the problem is that the default markdown parsing doesn't parse the content as link when space is included in the href. For example,
[Link](https://{%tag%})can be parsed, but[Link](https://{% tag %})can't be parsed.This is why I choose to add a plugin for the tokenizer and throw an error there. This can ensure the default parsing is not changed while additional validation can be enforced.
This link validation is optional. It can be enabled through
allowLinkValidationin the config. Custom protocol to validate (validatedProtocols) can be specified inlinkValidationOptions. It's default tohttpandhttps.