Skip to content

Disable markdoc tag in link href in tokenizer - #624

Merged
rayc-stripe merged 7 commits into
markdoc:mainfrom
rayc-stripe:chanyunh/link-href-disable-markdoc-tag
Jul 14, 2026
Merged

rayc-stripe merged 7 commits into
markdoc:mainfrom
rayc-stripe:chanyunh/link-href-disable-markdoc-tag

Conversation

@rayc-stripe

@rayc-stripe rayc-stripe commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

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 allowLinkValidation in the config. Custom protocol to validate (validatedProtocols) can be specified in linkValidationOptions. It's default to http and https.

@rayc-stripe
rayc-stripe force-pushed the chanyunh/link-href-disable-markdoc-tag branch from 806c32b to 74dd880 Compare March 31, 2026 20:53

@yue-stripe yue-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend reusing markdown-it's markdown AST instead of trying to parse markdown links ourselves

Comment thread src/tokenizer/plugins/link.ts Outdated
for (token of state.tokens) {
if (token.type !== 'inline' || typeof token.content !== 'string') continue;

const matches = token.content.match(MARKDOWN_LINK_DESTINATION) || [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/utils.ts Outdated

export const OPEN = '{%';
export const CLOSE = '%}';
export const TAG_PATTERN = /\{%\s*[\s\S]*?%\}/;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rayc-stripe
rayc-stripe merged commit 5000d94 into markdoc:main Jul 14, 2026
2 checks passed
@rayc-stripe rayc-stripe mentioned this pull request Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants