Skip to content

Commit

Permalink
fix: simplify match token check
Browse files Browse the repository at this point in the history
  • Loading branch information
main-kun authored and moki committed Nov 30, 2023
1 parent 19551c2 commit cb42b3f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/transform/plugins/block-anchor/block-anchor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import StateCore from 'markdown-it/lib/rules_core/state_core';
import Token from 'markdown-it/lib/token';

const pattern = /^{%[^\S\r\n]*anchor[^\S\r\n]+([\w-]+)[^\S\r\n]*%}$/;
const pattern = /^{%[^\S\r\n]*anchor[^\S\r\n]+([\w-]+)[^\S\r\n]*%}/;
export const TOKEN_NAME = 'anchor';

function matchOpenToken(tokens: Token[], i: number) {
return (
tokens[i].type === 'paragraph_open' &&
tokens[i + 1].type === 'inline' &&
tokens[i + 2].type === 'paragraph_close' &&
pattern.exec(tokens[i + 1].content)
tokens[i + 1].children?.length === 1 &&
tokens[i + 1].children?.[0].type === 'text' &&
pattern.exec(tokens[i + 1].children?.[0].content as string)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/transform/plugins/block-anchor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MarkdownIt from 'markdown-it';
import {renderTokens, replaceTokens, TOKEN_NAME} from './block-anchor';

const blockAnchor = (md: MarkdownIt) => {
md.core.ruler.push(TOKEN_NAME, replaceTokens);
md.core.ruler.before('curly_attributes', TOKEN_NAME, replaceTokens);
md.renderer.rules[TOKEN_NAME] = renderTokens;

return md;
Expand Down

0 comments on commit cb42b3f

Please sign in to comment.