Skip to content

Commit

Permalink
fix: revert JSX expression hack (temporary!)
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed May 25, 2022
1 parent f200dda commit b78883f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
1 change: 0 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
const { layout = '', components = '', setup = '', ...content } = frontmatter;
content.astro = metadata;
const prelude = `---
import { slug as $$slug } from '@astrojs/markdown-remark';
${layout ? `import Layout from '${layout}';` : ''}
${components ? `import * from '${components}';` : ''}
${hasInjectedScript ? `import '${PAGE_SSR_SCRIPT_ID}';` : ''}
Expand Down
14 changes: 4 additions & 10 deletions packages/markdown/remark/src/rehype-collect-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ export default function createCollectHeaders() {

node.properties = node.properties || {};
if (typeof node.properties.id !== 'string') {
if (isJSX) {
// 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';
(
node as any
).value = `<${node.tagName} id={${node.properties.id}}>${raw}</${node.tagName}>`;
} else {
node.properties.id = slugger.slug(text);
}
node.properties.id = slugger.slug(text);
// TODO: restore fix for IDs from JSX expressions
// Reverted due to https://github.com/withastro/astro/issues/3443
// See https://github.com/withastro/astro/pull/3410/files#diff-f0cc828ac662d9b8d48cbb9cb147883e319cdd8fa24f24ef401960520f1436caR44-R51
}

headers.push({ depth, slug: node.properties.id, text });
Expand Down
9 changes: 6 additions & 3 deletions packages/markdown/remark/test/expressions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@ describe('expressions', () => {
chai.expect(code).to.equal(`<Fragment>\n<Component>{a}</Component>\n</Fragment>`);
});

it('should be able to serialize expression inside markdown', async () => {
// TODO: remove skips when IDs-by-JSX-expressions are restored
// Reverted due to https://github.com/withastro/astro/issues/3443
// See https://github.com/withastro/astro/pull/3410/files#diff-f0cc828ac662d9b8d48cbb9cb147883e319cdd8fa24f24ef401960520f1436caR44-R51
it.skip('should be able to serialize expression inside markdown', async () => {
const { code } = await renderMarkdown(`# {frontmatter.title}`, {});

chai
.expect(code)
.to.equal(`<h1 id={$$slug(\`\${frontmatter.title}\`)}>{frontmatter.title}</h1>`);
});

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

chai
.expect(code)
.to.equal(`<h1 id={$$slug(\`Hello \${frontmatter.name}\`)}>Hello {frontmatter.name}</h1>`);
});

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

chai
Expand Down

0 comments on commit b78883f

Please sign in to comment.