-
Notifications
You must be signed in to change notification settings - Fork 18
feat(mdxish): introduce new HTMLBlock micromark tokenizer #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
27c7a48
f9788a0
631aebd
3001457
fb6450e
d804324
18a081d
47b87b1
26f1124
69def82
10d4f91
24390a9
012ebb8
989aa8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More tests suggestion:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
i dont think we should cover this case tho, even MDX themselve dont support this so i think this is just plain malformed and wrong syntax
added this one with a bit of a tweak! |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import type { Html } from 'mdast'; | ||
| import type { CompileContext, Extension as FromMarkdownExtension, Handle, Token } from 'mdast-util-from-markdown'; | ||
|
|
||
| const contextMap = new WeakMap<Token, { chunks: string[]; lastEndLine: number }>(); | ||
|
|
||
| function findHtmlBlockComponentToken(this: CompileContext): Token | undefined { | ||
| const events = this.tokenStack; | ||
| for (let i = events.length - 1; i >= 0; i -= 1) { | ||
| if (events[i][0].type === 'htmlBlockComponent') return events[i][0]; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| // Produces an MDAST `html` node, mdxishHtmlBlocks then transforms it into an `html-block` node | ||
| function enterHtmlBlockComponent(this: CompileContext, token: Parameters<Handle>[0]): void { | ||
| contextMap.set(token, { chunks: [], lastEndLine: token.start.line }); | ||
| this.enter({ type: 'html', value: '' } as Html, token); | ||
| } | ||
|
|
||
| function exitHtmlBlockComponentData(this: CompileContext, token: Parameters<Handle>[0]): void { | ||
| const componentToken = findHtmlBlockComponentToken.call(this); | ||
| if (!componentToken) return; | ||
| const ctx = contextMap.get(componentToken); | ||
| if (ctx) { | ||
| const gap = token.start.line - ctx.lastEndLine; | ||
| if (ctx.chunks.length > 0 && gap > 0) { | ||
| ctx.chunks.push('\n'.repeat(gap)); | ||
| } | ||
| ctx.chunks.push(this.sliceSerialize(token)); | ||
| ctx.lastEndLine = token.end.line; | ||
| } | ||
| } | ||
|
|
||
| function exitHtmlBlockComponent(this: CompileContext, token: Parameters<Handle>[0]): void { | ||
| const ctx = contextMap.get(token); | ||
| const node = this.stack[this.stack.length - 1] as Html; | ||
| if (ctx) { | ||
| node.value = ctx.chunks.join(''); | ||
| contextMap.delete(token); | ||
| } | ||
| this.exit(token); | ||
| } | ||
|
|
||
| export function htmlBlockComponentFromMarkdown(): FromMarkdownExtension { | ||
| return { | ||
| enter: { | ||
| htmlBlockComponent: enterHtmlBlockComponent, | ||
| }, | ||
| exit: { | ||
| htmlBlockComponentData: exitHtmlBlockComponentData, | ||
| htmlBlockComponent: exitHtmlBlockComponent, | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { htmlBlockComponent } from './syntax'; |
Uh oh!
There was an error while loading. Please reload this page.