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

Prevent header plugin from turning code blocks into expressions #3510

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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/tall-cats-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Prevents code blocks containing expression from being evaluated as such
6 changes: 5 additions & 1 deletion packages/markdown/remark/src/rehype-collect-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ export default function createCollectHeaders() {

let raw = '';
let text = '';
let elementTag: string | null = null;
let isJSX = false;
visit(node, (child) => {
if (child.type === 'element') {
elementTag = child.tagName;
return;
}
if (child.type === 'raw') {
Expand All @@ -34,7 +36,9 @@ export default function createCollectHeaders() {
if (child.type === 'text' || child.type === 'raw') {
raw += child.value;
text += child.value;
isJSX = isJSX || child.value.includes('{');
if(elementTag !== 'code') {
isJSX = isJSX || child.value.includes('{');
}
}
});

Expand Down
8 changes: 8 additions & 0 deletions packages/markdown/remark/test/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ describe('expressions', () => {
.to.equal(`<h1 id={$$slug(\`\${frontmatter.title}\`)}>{frontmatter.title}</h1>`);
});

it('should skip expressions inside of backtick blocks', async () => {
const { code } = await renderMarkdown(`# \`{frontmatter.title}\` and \`{another}\``, {});

chai
.expect(code)
.to.equal(`<h1 id="frontmattertitle-and-another"><code is:raw>{frontmatter.title}</code> and <code is:raw>{another}</code></h1>`);
});

it('should be able to serialize complex expression inside markdown', async () => {
const { code } = await renderMarkdown(`# Hello {frontmatter.name}`, {});

Expand Down