-
Notifications
You must be signed in to change notification settings - Fork 18
fix(stripComments): preserve HTMLBlock template literals in mdxish mode #1410
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
Draft
eaglethrost
wants to merge
8
commits into
next
Choose a base branch
from
dimas/rm-15880-broken-view-as-markdown-for-html-block
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63215eb
refactor: move html block extractor to its file & use it in strip com…
eaglethrost 344c6cd
fix: add backticks option to restorer
eaglethrost 57b314c
test: enhance
eaglethrost 6e6381f
fix: remove dead test
eaglethrost 72e2860
fix: update comments
eaglethrost 5d6b8d9
Merge branch 'next' into dimas/rm-15880-broken-view-as-markdown-for-h…
eaglethrost 55c21ad
style: comments
eaglethrost 068d911
Merge branch 'next' into dimas/rm-15880-broken-view-as-markdown-for-h…
eaglethrost 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
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,76 @@ | ||
| // Base64 encode (Node.js + browser compatible) | ||
| function base64Encode(str: string): string { | ||
| if (typeof Buffer !== 'undefined') { | ||
| return Buffer.from(str, 'utf-8').toString('base64'); | ||
| } | ||
| return btoa(unescape(encodeURIComponent(str))); | ||
| } | ||
|
|
||
| function base64Decode(str: string): string { | ||
| if (typeof Buffer !== 'undefined') { | ||
| return Buffer.from(str, 'base64').toString('utf-8'); | ||
| } | ||
| return decodeURIComponent(escape(atob(str))); | ||
| } | ||
|
|
||
| // Markers for protected HTMLBlock content | ||
| // Add some random characters to the markers to reduce likelihood of conflicts with other content | ||
| const HTML_BLOCK_CONTENT_START = 'RMDX-!#@-HTMLBLOCK-START%:'; | ||
| const HTML_BLOCK_CONTENT_END = ':%RMDX-!#@-HTMLBLOCK-END'; | ||
|
|
||
| /** | ||
| * Matches HTMLBlock template literal expressions: `<HTMLBlock>{` ... `}</HTMLBlock>` | ||
| */ | ||
| export const HTMLBLOCK_TEMPLATE_LITERAL_REGEX = /(<HTMLBlock[^>]*>)\{\s*`((?:[^`\\]|\\.)*)`\s*\}(<\/HTMLBlock>)/g; | ||
|
|
||
| /** | ||
| * Base64 encodes HTMLBlock template literal content to prevent markdown parser from consuming <script>/<style> tags. | ||
| * | ||
| * @param content | ||
| * @returns Content with HTMLBlock template literals base64 encoded in HTML comments | ||
| * @example | ||
| * ```typescript | ||
| * const input = '<HTMLBlock>{`<script>alert("xss")</script>`}</HTMLBlock>'; | ||
| * protectHTMLBlockContent(input) | ||
| * // Returns: '<HTMLBlock>RDMX-HTMLBLOCK-START:PHNjcmlwdD5hbGVydCgieHNzIik8L3NjcmlwdD4=:RDMX-HTMLBLOCK-END</HTMLBlock>' | ||
|
maximilianfalco marked this conversation as resolved.
Outdated
|
||
| * ``` | ||
| */ | ||
| export function protectHTMLBlockContent(content: string) { | ||
| return content.replace( | ||
| HTMLBLOCK_TEMPLATE_LITERAL_REGEX, | ||
| (_match, openTag: string, templateContent: string, closeTag: string) => { | ||
| const encoded = base64Encode(templateContent); | ||
| return `${openTag}${HTML_BLOCK_CONTENT_START}${encoded}${HTML_BLOCK_CONTENT_END}${closeTag}`; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Restores HTMLBlock content that was protected by `protectHTMLBlockContent`. | ||
| * When `withBackticks` is true, re-wraps the decoded body as `{`...`}` (used by `stripComments` to preserve | ||
| * original markup). When false or omitted, inserts decoded HTML only (typical for transformers). | ||
| * | ||
| * @param content | ||
| * @param withBackticks | ||
| * @returns Content with protected markers replaced by decoded HTML, optionally wrapped in template literal syntax | ||
| * @example | ||
| * ```typescript | ||
| * const input = | ||
| * '<HTMLBlock>RMDX-!#@-HTMLBLOCK-START%:PHNjcmlwdD5hbGVydCgieHNzIik8L3NjcmlwdD4=:RMDX-!#@-HTMLBLOCK-END</HTMLBlock>'; | ||
| * restoreHTMLBlockContent(input, true); | ||
| * // Returns: '<HTMLBlock>{`<script>alert("xss")</script>`}</HTMLBlock>' | ||
| * restoreHTMLBlockContent(input); | ||
| * // Returns: '<HTMLBlock><script>alert("xss")</script></HTMLBlock>' | ||
| * ``` | ||
| */ | ||
| export function restoreHTMLBlockContent(content: string, withBackticks?: boolean) { | ||
| const markerRegex = new RegExp(`${HTML_BLOCK_CONTENT_START}([A-Za-z0-9+/=]+)${HTML_BLOCK_CONTENT_END}`, 'g'); | ||
| return content.replace(markerRegex, (_match, encoded: string) => { | ||
| try { | ||
| const decoded = base64Decode(encoded); | ||
| return withBackticks ? `{\`${decoded}\`}` : decoded; | ||
| } catch { | ||
| return encoded; | ||
| } | ||
| }); | ||
| } | ||
|
maximilianfalco marked this conversation as resolved.
Outdated
|
||
File renamed without changes.
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
Oops, something went wrong.
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.