Skip to content
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 cases for JSX-like expressions in code blocks of headings #3502

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-humans-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': minor
---

Fix cases for JSX-like expressions in code blocks of headings
13 changes: 7 additions & 6 deletions packages/markdown/remark/src/rehype-collect-headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { visit } from 'unist-util-visit';
import { toHtml } from 'hast-util-to-html';
import Slugger from 'github-slugger';

import type { MarkdownHeader, RehypePlugin } from './types.js';
Expand All @@ -17,22 +18,18 @@ export default function createCollectHeaders() {
if (!level) return;
const depth = Number.parseInt(level);

let raw = '';
let text = '';
let isJSX = false;
visit(node, (child) => {
if (child.type === 'element') {
visit(node, (child, _, parent) => {
if (child.type === 'element' || ['code', 'pre'].some((tag) => parent?.tagName === tag)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use an object or Set here to avoid an unnecessary O(2) lookup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to use Set to scan parent.tagName in fb69531

return;
}
if (child.type === 'raw') {
// HACK: serialized JSX from internal plugins, ignore these for slug
if (child.value.startsWith('\n<') || child.value.endsWith('>\n')) {
raw += child.value.replace(/^\n|\n$/g, '');
return;
}
}
if (child.type === 'text' || child.type === 'raw') {
raw += child.value;
text += child.value;
isJSX = isJSX || child.value.includes('{');
}
Expand All @@ -41,6 +38,10 @@ export default function createCollectHeaders() {
node.properties = node.properties || {};
if (typeof node.properties.id !== 'string') {
if (isJSX) {
// HACK: serialized JSX from internal plugins, ignore these for slug
const raw = toHtml(node.children, { allowDangerousHtml: true })
.replace(/\n(<)/g, '<')
.replace(/(>)\n/g, '>');
// HACK: for ids that have JSX content, use $$slug helper to generate slug at runtime
node.properties.id = `$$slug(\`${text.replace(/\{/g, '${')}\`)`;
(node as any).type = 'raw';
Expand Down
25 changes: 24 additions & 1 deletion packages/markdown/remark/test/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderMarkdown } from '../dist/index.js';
import chai from 'chai';

describe('expressions', () => {
it('should be able to serialize bare expession', async () => {
it('should be able to serialize bare expression', async () => {
const { code } = await renderMarkdown(`{a}`, {});

chai.expect(code).to.equal(`{a}`);
Expand Down Expand Up @@ -40,6 +40,29 @@ describe('expressions', () => {
);
});

it('should be able to avoid evaluating JSX-like expressions in inline codes', async () => {
const { code } = await renderMarkdown(`# \`{ foo }\` is a shorthand for \`{ foo: foo }\``, {});

chai
.expect(code)
.to.equal(
'<h1 id="-is-a-shorthand-for-"><code is:raw>{ foo }</code> is a shorthand for <code is:raw>{ foo: foo }</code></h1>'
);
});

it('should be able to avoid evaluating JSX-like expressions & escape HTML tag characters in inline codes', async () => {
const { code } = await renderMarkdown(
`###### \`{}\` is equivalent to \`Record<never, never>\` <small>(at TypeScript v{frontmatter.version})</small>`,
{}
);

chai
.expect(code)
.to.equal(
`<h6 id={$$slug(\` is equivalent to (at TypeScript v\${frontmatter.version})\`)}><code is:raw>{}</code> is equivalent to <code is:raw>Record&lt;never, never&gt;</code> <small>(at TypeScript v{frontmatter.version})</small></h6>`
);
});

it('should be able to serialize function expression', async () => {
const { code } = await renderMarkdown(
`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`,
Expand Down