vscode-markdown
vscode-markdown copied to clipboard
Fix decoration for code spans in task lists
Fixes #1067
When t.type === "html_inline"
, t.content
is a HTML tag, but https://github.com/yy0931/vscode-markdown/blob/b7cf0acf56abff059c5c71bc98b9446b62b708d4/src/theming/decorationWorkerRegistry.ts#L68 is assuming that t.content
is a markdown (e.g. "- [ ]"
).
Many thanks 👍. It looks alright to me.
cc @Lemmingh
~~I believe we should we should move to micromark instead of fixing markdown-it-task-lists. With micromark we can get the exact location of each token and it is much easier to maintain. In addition to that, it will be possible to decorate code spans in tables.~~
import { preprocess } from "micromark/lib/preprocess"
import { parse } from "micromark/lib/parse"
import { postprocess } from "micromark/lib/postprocess"
import { gfm } from "micromark-extension-gfm"
const content =
"- [x] \n" +
" ``` `alpha` ```\n"
for (const [eventType, token, _tokenizeContext] of postprocess(parse({ extensions: [gfm()] }).document().write(preprocess()(content, undefined, true)))) {
if (eventType === "enter") {
if (token.type === "codeText") {
console.assert(content.slice(token.start.offset, token.end.offset) === "``` `alpha` ```")
} else if (token.type === "codeTextData") {
console.assert(content.slice(token.start.offset, token.end.offset) === "`alpha`")
} else if (token.type === "codeTextPadding") {
console.assert(content.slice(token.start.offset, token.end.offset) === " ")
} else if (token.type === "codeTextSequence") {
console.assert(content.slice(token.start.offset, token.end.offset) === "```")
}
}
}
@Lemmingh
My comment above was missing the point. I agree with you that skipping html_inline
won't fix the problem entirely. I also thought about fixing markdown-it (and its plugins) but there should be so many changes needed that I thought it would be better to use another markdown parser.
This pull request is a temporary fix and prevents the current cursor position (beginOffset
) from being after the actual token. Doing so will not fix the problem entirely, but it will alleviate the problem because the indexOf()
on the next token
https://github.com/yzhang-gh/vscode-markdown/blob/b7cf0acf56abff059c5c71bc98b9446b62b708d4/src/theming/decorationWorkerRegistry.ts#L74-L83
will move beginOffset
to the correct position.
Ultimately, we need to make changes to the parser as you mentioned.