-
Notifications
You must be signed in to change notification settings - Fork 13.3k
fix: add remark plugin to render raw HTML as literal text #16505
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
Merged
allozaur
merged 3 commits into
ggml-org:master
from
ServeurpersoCom:fix-markdown-literal-html
Oct 13, 2025
+235
−4
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
ServeurpersoCom marked this conversation as resolved.
Show resolved
Hide resolved
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,15 @@ | ||
export const LINE_BREAK = /\r?\n/; | ||
|
||
export const PHRASE_PARENTS = new Set([ | ||
'paragraph', | ||
'heading', | ||
'emphasis', | ||
'strong', | ||
'delete', | ||
'link', | ||
'linkReference', | ||
'tableCell' | ||
]); | ||
|
||
export const NBSP = '\u00a0'; | ||
export const TAB_AS_SPACES = NBSP.repeat(4); |
ServeurpersoCom marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,121 @@ | ||
import type { Plugin } from 'unified'; | ||
import { visit } from 'unist-util-visit'; | ||
import type { Break, Content, Paragraph, PhrasingContent, Root, Text } from 'mdast'; | ||
import { LINE_BREAK, NBSP, PHRASE_PARENTS, TAB_AS_SPACES } from '$lib/constants/literal-html'; | ||
|
||
/** | ||
* remark plugin that rewrites raw HTML nodes into plain-text equivalents. | ||
* | ||
* remark parses inline HTML into `html` nodes even when we do not want to render | ||
* them. We turn each of those nodes into regular text (plus `<br>` break markers) | ||
* so the downstream rehype pipeline escapes the characters instead of executing | ||
* them. Leading spaces and tab characters are converted to non‑breaking spaces to | ||
* keep indentation identical to the original author input. | ||
*/ | ||
|
||
function preserveIndent(line: string): string { | ||
let index = 0; | ||
let output = ''; | ||
|
||
while (index < line.length) { | ||
const char = line[index]; | ||
|
||
if (char === ' ') { | ||
output += NBSP; | ||
index += 1; | ||
continue; | ||
} | ||
|
||
if (char === '\t') { | ||
output += TAB_AS_SPACES; | ||
index += 1; | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
|
||
return output + line.slice(index); | ||
} | ||
|
||
function createLiteralChildren(value: string): PhrasingContent[] { | ||
const lines = value.split(LINE_BREAK); | ||
const nodes: PhrasingContent[] = []; | ||
|
||
for (const [lineIndex, rawLine] of lines.entries()) { | ||
if (lineIndex > 0) { | ||
nodes.push({ type: 'break' } as Break as unknown as PhrasingContent); | ||
} | ||
|
||
nodes.push({ | ||
type: 'text', | ||
value: preserveIndent(rawLine) | ||
} as Text as unknown as PhrasingContent); | ||
} | ||
|
||
if (!nodes.length) { | ||
nodes.push({ type: 'text', value: '' } as Text as unknown as PhrasingContent); | ||
} | ||
|
||
return nodes; | ||
} | ||
|
||
export const remarkLiteralHtml: Plugin<[], Root> = () => { | ||
return (tree) => { | ||
visit(tree, 'html', (node, index, parent) => { | ||
if (!parent || typeof index !== 'number') { | ||
return; | ||
} | ||
|
||
const replacement = createLiteralChildren(node.value); | ||
|
||
if (!PHRASE_PARENTS.has(parent.type as string)) { | ||
const paragraph: Paragraph = { | ||
type: 'paragraph', | ||
children: replacement as Paragraph['children'], | ||
data: { literalHtml: true } | ||
}; | ||
|
||
const siblings = parent.children as unknown as Content[]; | ||
siblings.splice(index, 1, paragraph as unknown as Content); | ||
|
||
if (index > 0) { | ||
const previous = siblings[index - 1] as Paragraph | undefined; | ||
|
||
if ( | ||
previous?.type === 'paragraph' && | ||
(previous.data as { literalHtml?: boolean } | undefined)?.literalHtml | ||
) { | ||
const prevChildren = previous.children as unknown as PhrasingContent[]; | ||
|
||
if (prevChildren.length) { | ||
const lastChild = prevChildren[prevChildren.length - 1]; | ||
|
||
if (lastChild.type !== 'break') { | ||
prevChildren.push({ | ||
type: 'break' | ||
} as Break as unknown as PhrasingContent); | ||
} | ||
} | ||
|
||
prevChildren.push(...(paragraph.children as unknown as PhrasingContent[])); | ||
|
||
siblings.splice(index, 1); | ||
|
||
return index; | ||
} | ||
} | ||
|
||
return index + 1; | ||
} | ||
|
||
(parent.children as unknown as PhrasingContent[]).splice( | ||
index, | ||
1, | ||
...(replacement as unknown as PhrasingContent[]) | ||
); | ||
|
||
return index + replacement.length; | ||
}); | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.