-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Fix rtl text rendering #21382
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
Fix rtl text rendering #21382
Changes from 4 commits
cda9e93
4a95f06
b080262
8ba36d0
d3b8a9f
4df0364
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||
| import { rehypeEnhanceLinks } from '$lib/markdown/enhance-links'; | ||||||
| import { rehypeEnhanceCodeBlocks } from '$lib/markdown/enhance-code-blocks'; | ||||||
| import { rehypeResolveAttachmentImages } from '$lib/markdown/resolve-attachment-images'; | ||||||
| import { rehypeRtlSupport } from '$lib/markdown/rehype-rtl-support'; | ||||||
| import { remarkLiteralHtml } from '$lib/markdown/literal-html'; | ||||||
| import { copyCodeToClipboard, preprocessLaTeX, getImageErrorFallbackHtml } from '$lib/utils'; | ||||||
| import { | ||||||
|
|
@@ -101,6 +102,7 @@ | |||||
| .use(rehypeEnhanceLinks) // Add target="_blank" to links | ||||||
| .use(rehypeEnhanceCodeBlocks) // Wrap code blocks with header and actions | ||||||
| .use(rehypeResolveAttachmentImages, { attachments }) | ||||||
| .use(rehypeRtlSupport) // Add bidirectional text support | ||||||
| .use(rehypeStringify, { allowDangerousHtml: true }); // Convert to HTML string | ||||||
| }); | ||||||
|
|
||||||
|
|
@@ -221,6 +223,8 @@ | |||||
| return previousContent.length > 0 && newContent.startsWith(previousContent); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Transforms a single MDAST node to HTML string with caching. | ||||||
| * Runs the full remark/rehype plugin pipeline (GFM, math, syntax highlighting, etc.) | ||||||
|
|
@@ -245,12 +249,13 @@ | |||||
|
|
||||||
| const singleNodeRoot = { type: 'root', children: [node] }; | ||||||
| const transformedRoot = (await processorInstance.run(singleNodeRoot as MdastRoot)) as HastRoot; | ||||||
| const html = processorInstance.stringify(transformedRoot); | ||||||
| let html = processorInstance.stringify(transformedRoot); | ||||||
|
Kabir08 marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| transformCache.set(hash, html); | ||||||
|
|
||||||
| return { html, hash }; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Handles click events on copy buttons within code blocks. | ||||||
|
|
@@ -597,16 +602,22 @@ | |||||
| class="{className}{config()[SETTINGS_KEYS.FULL_HEIGHT_CODE_BLOCKS] | ||||||
| ? ' full-height-code-blocks' | ||||||
| : ''}" | ||||||
| dir="auto" | ||||||
|
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.
Suggested change
No need to have additional attributes if we already have this handled by rehype plugin |
||||||
| > | ||||||
| {#each renderedBlocks as block (block.id)} | ||||||
| <div class="markdown-block" data-block-id={block.id} use:fadeInView={{ skipIfVisible: true }}> | ||||||
| <div | ||||||
| class="markdown-block" | ||||||
| data-block-id={block.id} | ||||||
| use:fadeInView={{ skipIfVisible: true }} | ||||||
| dir="auto" | ||||||
|
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.
Suggested change
|
||||||
| > | ||||||
| <!-- eslint-disable-next-line no-at-html-tags --> | ||||||
| {@html block.html} | ||||||
| </div> | ||||||
| {/each} | ||||||
|
|
||||||
| {#if unstableBlockHtml} | ||||||
| <div class="markdown-block markdown-block--unstable" data-block-id="unstable"> | ||||||
| <div class="markdown-block markdown-block--unstable" data-block-id="unstable" dir="auto"> | ||||||
|
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.
Suggested change
|
||||||
| <!-- eslint-disable-next-line no-at-html-tags --> | ||||||
| {@html unstableBlockHtml} | ||||||
| </div> | ||||||
|
|
@@ -738,9 +749,8 @@ | |||||
| padding: 0.125rem 0.375rem; | ||||||
| border-radius: 0.375rem; | ||||||
| font-size: 0.875rem; | ||||||
| font-family: | ||||||
| ui-monospace, SFMono-Regular, 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, | ||||||
| 'Liberation Mono', Menlo, monospace; | ||||||
| font-family: ui-monospace, SFMono-Regular, 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', | ||||||
| Consolas, 'Liberation Mono', Menlo, monospace; | ||||||
| } | ||||||
|
|
||||||
| div :global(pre) { | ||||||
|
|
@@ -781,19 +791,19 @@ | |||||
| /* Lists */ | ||||||
| div :global(ul) { | ||||||
| list-style-type: disc; | ||||||
| margin-left: 1.5rem; | ||||||
| margin-inline-start: 1.5rem; | ||||||
| margin-bottom: 1rem; | ||||||
| } | ||||||
|
|
||||||
| div :global(ol) { | ||||||
| list-style-type: decimal; | ||||||
| margin-left: 1.5rem; | ||||||
| margin-inline-start: 1.5rem; | ||||||
| margin-bottom: 1rem; | ||||||
| } | ||||||
|
|
||||||
| div :global(li) { | ||||||
| margin-bottom: 0.25rem; | ||||||
| padding-left: 0.5rem; | ||||||
| padding-inline-start: 0.5rem; | ||||||
| } | ||||||
|
|
||||||
| div :global(li::marker) { | ||||||
|
|
@@ -816,8 +826,8 @@ | |||||
| /* Task lists */ | ||||||
| div :global(.task-list-item) { | ||||||
| list-style: none; | ||||||
| margin-left: 0; | ||||||
| padding-left: 0; | ||||||
| margin-inline-start: 0; | ||||||
| padding-inline-start: 0; | ||||||
| } | ||||||
|
|
||||||
| div :global(.task-list-item-checkbox) { | ||||||
|
|
@@ -942,9 +952,8 @@ | |||||
| div :global(.code-language) { | ||||||
| color: var(--color-foreground); | ||||||
| font-weight: 500; | ||||||
| font-family: | ||||||
| ui-monospace, SFMono-Regular, 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', Consolas, | ||||||
| 'Liberation Mono', Menlo, monospace; | ||||||
| font-family: ui-monospace, SFMono-Regular, 'SF Mono', Monaco, 'Cascadia Code', 'Roboto Mono', | ||||||
| Consolas, 'Liberation Mono', Menlo, monospace; | ||||||
| text-transform: uppercase; | ||||||
| font-size: 0.75rem; | ||||||
| letter-spacing: 0.05em; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Rehype plugin to provide comprehensive RTL support by adding dir="auto" | ||
| * to all text-containing elements. | ||
| * | ||
| * This operates directly on the HAST tree, ensuring that all elements | ||
| * (including those not in a predefined list) receive the attribute. | ||
| */ | ||
|
|
||
| import type { Plugin } from 'unified'; | ||
| import type { Root, Element } from 'hast'; | ||
| import { visit } from 'unist-util-visit'; | ||
|
|
||
| /** | ||
| * Rehype plugin to add dir="auto" to all elements that have children. | ||
| * This provides bidirectional text support for mixed RTL/LTR content. | ||
| */ | ||
| export const rehypeRtlSupport: Plugin<[], Root> = () => { | ||
| return (tree: Root) => { | ||
| visit(tree, 'element', (node: Element) => { | ||
| if (node.children && node.children.length > 0) { | ||
| node.properties = { | ||
| ...node.properties, | ||
| dir: 'auto' | ||
| }; | ||
| } | ||
| }); | ||
| }; | ||
| }; |
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.
redundant empty lines