-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: match anchors within paragraphs
- Loading branch information
Showing
5 changed files
with
67 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
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]*%}$/; | ||
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) | ||
); | ||
} | ||
|
||
function createAnchorToken(state: StateCore, anchorId: string, position: number) { | ||
const token = new state.Token(TOKEN_NAME, '', 0); | ||
token.map = state.tokens[position].map; | ||
token.markup = state.tokens[position].markup; | ||
token.content = anchorId; | ||
return token; | ||
} | ||
|
||
export function replaceTokens(state: StateCore) { | ||
const blockTokens = state.tokens; | ||
// i hate the idea of splicing the array while we're iterating over it | ||
// so first lets find all the places where we will need to splice it and then actually do the splicing | ||
const splicePointsMap: Map<number, string> = new Map(); | ||
for (let i = 0; i < blockTokens.length; i++) { | ||
const match = matchOpenToken(blockTokens, i); | ||
|
||
if (!match) { | ||
continue; | ||
} | ||
|
||
splicePointsMap.set(i, match[1]); | ||
} | ||
splicePointsMap.forEach((anchorId, position) => { | ||
blockTokens.splice(position, 3, createAnchorToken(state, anchorId, position)); | ||
}); | ||
} | ||
|
||
export function renderTokens(tokens: Token[], idx: number) { | ||
const token = tokens[idx]; | ||
const id = token.content; | ||
return `<a id=${id}></a>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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.renderer.rules[TOKEN_NAME] = renderTokens; | ||
|
||
return md; | ||
}; | ||
|
||
export default blockAnchor; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters