diff options
author | 2022-05-24 17:02:11 -0500 | |
---|---|---|
committer | 2022-05-24 17:02:11 -0500 | |
commit | cfae9760b252052b6189e96398b819a4337634a8 (patch) | |
tree | e98714849c454ee4da35f788d8204aee143a52d1 /packages/markdown/remark/test | |
parent | 78e962f744a495b587bc691ad6b109543a5a5dde (diff) | |
download | astro-cfae9760b252052b6189e96398b819a4337634a8.tar.gz astro-cfae9760b252052b6189e96398b819a4337634a8.tar.zst astro-cfae9760b252052b6189e96398b819a4337634a8.zip |
Improve Markdown + Components usage (#3410)
* feat: use internal MDX tooling for markdown + components
* fix: improve MD + component tests
* chore: add changeset
* fix: make tsc happy
* fix(#3319): add regression test for component children
* fix(markdown): support HTML comments in markdown
* fix(#2474): ensure namespaced components are properly handled in markdown pages
* fix(#3220): ensure html in markdown pages does not have extra surrounding space
* fix(#3264): ensure that remark files pass in file information
* fix(#3254): enable experimentalStaticExtraction for `.md` pages
* fix: revert parsing change
* fix: remove `markdown.mode` option
Diffstat (limited to 'packages/markdown/remark/test')
-rw-r--r-- | packages/markdown/remark/test/components.test.js | 62 | ||||
-rw-r--r-- | packages/markdown/remark/test/expressions.test.js | 40 | ||||
-rw-r--r-- | packages/markdown/remark/test/plugins.test.js | 26 |
3 files changed, 128 insertions, 0 deletions
diff --git a/packages/markdown/remark/test/components.test.js b/packages/markdown/remark/test/components.test.js new file mode 100644 index 000000000..0f3418c24 --- /dev/null +++ b/packages/markdown/remark/test/components.test.js @@ -0,0 +1,62 @@ +import { renderMarkdown } from '../dist/index.js'; +import chai from 'chai'; + +describe('components', () => { + it('should be able to serialize string', async () => { + const { code } = await renderMarkdown(`<Component str="cool!" />`, {}); + + chai.expect(code).to.equal(`<Component str="cool!" />`); + }); + + it('should be able to serialize boolean attribute', async () => { + const { code } = await renderMarkdown(`<Component bool={true} />`, {}); + + chai.expect(code).to.equal(`<Component bool={true} />`); + }); + + it('should be able to serialize array', async () => { + const { code } = await renderMarkdown(`<Component prop={["a", "b", "c"]} />`, {}); + + chai.expect(code).to.equal(`<Component prop={["a", "b", "c"]} />`); + }); + + it('should be able to serialize object', async () => { + const { code } = await renderMarkdown(`<Component prop={{ a: 0, b: 1, c: 2 }} />`, {}); + + chai.expect(code).to.equal(`<Component prop={{ a: 0, b: 1, c: 2 }} />`); + }); + + it('should be able to serialize empty attribute', async () => { + const { code } = await renderMarkdown(`<Component empty />`, {}); + + chai.expect(code).to.equal(`<Component empty />`); + }); + + // Notable omission: shorthand attribute + + it('should be able to serialize spread attribute', async () => { + const { code } = await renderMarkdown(`<Component {...spread} />`, {}); + + chai.expect(code).to.equal(`<Component {...spread} />`); + }); + + it('should allow client:* directives', async () => { + const { code } = await renderMarkdown(`<Component client:load />`, {}); + + chai.expect(code).to.equal(`<Component client:load />`); + }); + + it('should normalize children', async () => { + const { code } = await renderMarkdown(`<Component bool={true}>Hello world!</Component>`, {}); + + chai.expect(code).to.equal(`<Fragment>\n<Component bool={true}>Hello world!</Component>\n</Fragment>`); + }); + + it('should allow markdown without many spaces', async () => { + const { code } = await renderMarkdown(`<Component> +# Hello world! +</Component>`, {}); + + chai.expect(code).to.equal(`<Fragment>\n<Component><h1 id="hello-world">Hello world!</h1></Component>\n</Fragment>`); + }); +}); diff --git a/packages/markdown/remark/test/expressions.test.js b/packages/markdown/remark/test/expressions.test.js new file mode 100644 index 000000000..bcc95cbed --- /dev/null +++ b/packages/markdown/remark/test/expressions.test.js @@ -0,0 +1,40 @@ +import { renderMarkdown } from '../dist/index.js'; +import chai from 'chai'; + +describe('expressions', () => { + it('should be able to serialize bare expession', async () => { + const { code } = await renderMarkdown(`{a}`, {}); + + chai.expect(code).to.equal(`{a}`); + }); + + it('should be able to serialize expression inside component', async () => { + const { code } = await renderMarkdown(`<Component>{a}</Component>`, {}); + + chai.expect(code).to.equal(`<Fragment>\n<Component>{a}</Component>\n</Fragment>`); + }); + + it('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 () => { + 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 () => { + const { code } = await renderMarkdown(`# Hello <span>{frontmatter.name}</span>`, {}); + + chai.expect(code).to.equal(`<h1 id={$$slug(\`Hello \${frontmatter.name}\`)}>Hello <span>{frontmatter.name}</span></h1>`); + }); + + it('should be able to serialize function expression', async () => { + const { code } = await renderMarkdown(`{frontmatter.list.map(item => <p id={item}>{item}</p>)}` , {}); + + chai.expect(code).to.equal(`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`); + }) +}); diff --git a/packages/markdown/remark/test/plugins.test.js b/packages/markdown/remark/test/plugins.test.js new file mode 100644 index 000000000..4954047b5 --- /dev/null +++ b/packages/markdown/remark/test/plugins.test.js @@ -0,0 +1,26 @@ +import { renderMarkdown } from '../dist/index.js'; +import chai from 'chai'; + +import { fileURLToPath } from 'node:url'; + +describe('plugins', () => { + // https://github.com/withastro/astro/issues/3264 + it('should be able to get file path when passing fileURL', async () => { + let context; + await renderMarkdown(`test`, { + fileURL: new URL('virtual.md', import.meta.url), + remarkPlugins: [ + function () { + const transformer = (tree, file) => { + context = file; + }; + + return transformer; + } + ] + }); + + chai.expect(typeof context).to.equal('object'); + chai.expect(context.path).to.equal(fileURLToPath(new URL('virtual.md', import.meta.url))); + }); +}) |