forked from withastro/starlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve inline code and code block support in RTL languages (withastr…
…o#525) Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
- Loading branch information
1 parent
e287d6f
commit 87caf21
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@astrojs/starlight': patch | ||
--- | ||
|
||
Improve inline code and code block support in RTL languages |
25 changes: 25 additions & 0 deletions
25
packages/starlight/__tests__/remark-rehype/code-rtl-support.test.ts
This file contains 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,25 @@ | ||
import { rehype } from 'rehype'; | ||
import { expect, test } from 'vitest'; | ||
import { rehypeRtlCodeSupport } from '../../integrations/code-rtl-support'; | ||
|
||
const processor = rehype().data('settings', { fragment: true }).use(rehypeRtlCodeSupport()); | ||
|
||
test('applies `dir="auto"` to inline code', async () => { | ||
const input = `<p>Some text with <code>inline code</code>.</p>`; | ||
const output = String(await processor.process(input)); | ||
expect(output).not.toEqual(input); | ||
expect(output).includes('dir="auto"'); | ||
expect(output).toMatchInlineSnapshot( | ||
'"<p>Some text with <code dir=\\"auto\\">inline code</code>.</p>"' | ||
); | ||
}); | ||
|
||
test('applies `dir="ltr"` to code blocks', async () => { | ||
const input = `<p>Some text in a paragraph:</p><pre><code>console.log('test')</code></pre>`; | ||
const output = String(await processor.process(input)); | ||
expect(output).not.toEqual(input); | ||
expect(output).includes('dir="ltr"'); | ||
expect(output).toMatchInlineSnapshot( | ||
'"<p>Some text in a paragraph:</p><pre dir=\\"ltr\\"><code>console.log(\'test\')</code></pre>"' | ||
); | ||
}); |
This file contains 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,3 @@ | ||
import { defineVitestConfig } from '../test-config'; | ||
|
||
export default defineVitestConfig({ title: 'Remark-Rehype' }); |
This file contains 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 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,31 @@ | ||
import type { Root } from 'hastscript/lib/core'; | ||
import { CONTINUE, SKIP, visit } from 'unist-util-visit'; | ||
|
||
/** | ||
* rehype plugin that adds `dir` attributes to `<code>` and `<pre>` | ||
* elements that don’t already have them. | ||
* | ||
* `<code>` will become `<code dir="auto">` | ||
* `<pre>` will become `<pre dir="ltr">` | ||
* | ||
* `<code>` _inside_ `<pre>` is skipped, so respects the `ltr` on its parent. | ||
* | ||
* Reasoning: | ||
* - `<pre>` is usually a code block and code should be LTR even in an RTL document | ||
* - `<code>` is often LTR, but could also be RTL. `dir="auto"` ensures the bidirectional | ||
* algorithm treats the contents of `<code>` in isolation and gives its best guess. | ||
*/ | ||
export function rehypeRtlCodeSupport() { | ||
return () => (root: Root) => { | ||
visit(root, 'element', (el) => { | ||
if (el.tagName === 'pre' || el.tagName === 'code') { | ||
el.properties ||= {}; | ||
if (!('dir' in el.properties)) { | ||
el.properties.dir = { pre: 'ltr', code: 'auto' }[el.tagName]; | ||
} | ||
return SKIP; | ||
} | ||
return CONTINUE; | ||
}); | ||
}; | ||
} |