-
Notifications
You must be signed in to change notification settings - Fork 48
fix(context): preserve language fences in formatted pre/code blocks #180
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@neuledge/context": patch | ||
| --- | ||
|
|
||
| Preserve language fences and code indentation when HTML pre/code blocks contain surrounding formatting whitespace or comments. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,17 +40,31 @@ for (const tag of REMOVED_TAGS) { | |
|
|
||
| // DocBook emits bare <pre> elements; Turndown's code rule requires <pre><code>. | ||
| // Preserve their whitespace and prevent Markdown escaping of unit-file examples. | ||
| // Also recognize a sole <code> child preceded by whitespace or comments. | ||
| turndown.addRule("barePre", { | ||
| filter: (node) => | ||
|
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. Behavior · comment: The custom rule excludes This codebase is managed by Human0. |
||
| node.nodeName === "PRE" && node.firstChild?.nodeName !== "CODE", | ||
| replacement: (_content, node) => { | ||
| const code: string = node.textContent ?? ""; | ||
| const codeElement = node.firstElementChild; | ||
| const isWrappedCode = | ||
| codeElement?.nodeName === "CODE" && | ||
| [...node.childNodes].every( | ||
| (child) => | ||
| child === codeElement || | ||
| (child.nodeType === 3 && !/\S/.test(child.textContent ?? "")) || | ||
|
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. Maintainability · comment: Use This codebase is managed by Human0. |
||
| child.nodeType === 8, | ||
| ); | ||
| const className = isWrappedCode | ||
| ? (codeElement.getAttribute("class") ?? "") | ||
| : ""; | ||
| const language = className.match(/\blanguage-(\S+)/)?.[1] ?? ""; | ||
| const code: string = (isWrappedCode ? codeElement : node).textContent ?? ""; | ||
| const longestRun = (code.match(/`+/g) ?? []).reduce( | ||
| (longest, run) => Math.max(longest, run.length), | ||
| 0, | ||
| ); | ||
| const fence = "`".repeat(Math.max(3, longestRun + 1)); | ||
| return `\n\n${fence}\n${code.replace(/\n$/, "")}\n${fence}\n\n`; | ||
| return `\n\n${fence}${language}\n${code.replace(/\n$/, "")}\n${fence}\n\n`; | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maintainability · comment: Add why this case exists: whitespace or comments before
<code>prevent Turndown’s built-in first-child rule from recognizing the language. That rationale explains when this special case is needed.This codebase is managed by Human0.