parent
dd147c390a
commit
f881a03961
15 changed files with 85 additions and 163 deletions
|
@ -1,5 +1,6 @@
|
|||
---
|
||||
import { renderMarkdown } from '@astrojs/markdown-remark';
|
||||
import stripIndent from 'strip-indent';
|
||||
|
||||
export interface Props {
|
||||
content?: string;
|
||||
|
@ -10,23 +11,22 @@ interface InternalProps extends Props {
|
|||
$scope: string;
|
||||
}
|
||||
|
||||
const { content, $scope } = Astro.props as InternalProps;
|
||||
let { content, $scope } = Astro.props as InternalProps;
|
||||
let html = null;
|
||||
|
||||
// This flow is only triggered if a user passes `<Markdown content={content} />`
|
||||
if (content) {
|
||||
const { content: htmlContent } = await renderMarkdown(content, {
|
||||
mode: 'md',
|
||||
$: {
|
||||
scopedClassName: $scope
|
||||
}
|
||||
});
|
||||
html = htmlContent;
|
||||
// If no content prop provided, use the slot.
|
||||
if(!content) {
|
||||
const renderSlot = (Astro as any).privateRenderSlotDoNotUse;
|
||||
content = stripIndent(await renderSlot('default'));
|
||||
}
|
||||
|
||||
/*
|
||||
If we have rendered `html` for `content`, render that
|
||||
Otherwise, just render the slotted content
|
||||
*/
|
||||
const { code: htmlContent } = await renderMarkdown(content, {
|
||||
mode: 'md',
|
||||
$: {
|
||||
scopedClassName: $scope
|
||||
}
|
||||
});
|
||||
|
||||
html = htmlContent;
|
||||
---
|
||||
{html ? html : <slot />}
|
||||
{html ? html : <slot />}
|
|
@ -1,17 +1,4 @@
|
|||
// export { default as Code } from './Code.astro';
|
||||
// export { default as Debug } from './Debug.astro';
|
||||
// export { default as Markdown } from './Markdown.astro';
|
||||
// export { default as Prism } from './Prism.astro';
|
||||
|
||||
export const Code = () => {
|
||||
throw new Error(`Cannot render <Code />. "astro/components" are still WIP!`)
|
||||
}
|
||||
export const Debug = () => {
|
||||
throw new Error(`Cannot render <Debug />. "astro/components" are still WIP!`)
|
||||
}
|
||||
export const Markdown = () => {
|
||||
throw new Error(`Cannot render <Markdown />. "astro/components" are still WIP!`)
|
||||
}
|
||||
export const Prism = () => {
|
||||
throw new Error(`Cannot render <Prism />. "astro/components" are still WIP!`)
|
||||
}
|
||||
export { default as Markdown } from './Markdown.astro';
|
||||
// export { default as Prism } from './Prism.astro';
|
|
@ -79,6 +79,7 @@
|
|||
"morphdom": "^2.6.1",
|
||||
"node-fetch": "^2.6.5",
|
||||
"path-to-regexp": "^6.2.0",
|
||||
"remark-slug": "^7.0.0",
|
||||
"sass": "^1.43.2",
|
||||
"semver": "^7.3.5",
|
||||
"send": "^0.17.1",
|
||||
|
@ -88,6 +89,7 @@
|
|||
"sourcemap-codec": "^1.4.8",
|
||||
"string-width": "^5.0.0",
|
||||
"strip-ansi": "^7.0.1",
|
||||
"strip-indent": "^4.0.0",
|
||||
"supports-esm": "^1.0.0",
|
||||
"tiny-glob": "^0.2.8",
|
||||
"vite": "^2.6.7",
|
||||
|
|
|
@ -9,7 +9,7 @@ import * as eslexer from 'es-module-lexer';
|
|||
import { fileURLToPath } from 'url';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { renderPage } from '../../runtime/server/index.js';
|
||||
import { renderPage, renderSlot } from '../../runtime/server/index.js';
|
||||
import { parseNpmName, canonicalURL as getCanonicalURL, codeFrame } from '../util.js';
|
||||
import { generatePaginateFunction } from './paginate.js';
|
||||
import { getParams, validateGetStaticPathsModule, validateGetStaticPathsResult } from './routing.js';
|
||||
|
@ -144,6 +144,9 @@ export async function ssr({ astroConfig, filePath, logging, mode, origin, pathna
|
|||
url,
|
||||
},
|
||||
slots: Object.fromEntries(Object.entries(slots || {}).map(([slotName]) => [slotName, true])),
|
||||
privateRenderSlotDoNotUse(slotName: string) {
|
||||
return renderSlot(result, slots ? slots[slotName] : null)
|
||||
}
|
||||
} as unknown as AstroGlobal;
|
||||
},
|
||||
_metadata: { renderers },
|
||||
|
|
|
@ -43,7 +43,7 @@ async function _render(child: any): Promise<any> {
|
|||
return child;
|
||||
} else if (!child && child !== 0) {
|
||||
// do nothing, safe to ignore falsey values.
|
||||
} else if (child instanceof AstroComponent) {
|
||||
} else if (child instanceof AstroComponent || child.toString() === '[object AstroComponent]') {
|
||||
return await renderAstroComponent(child);
|
||||
} else {
|
||||
return child;
|
||||
|
@ -59,6 +59,10 @@ export class AstroComponent {
|
|||
this.expressions = expressions;
|
||||
}
|
||||
|
||||
get [Symbol.toStringTag]() {
|
||||
return 'AstroComponent';
|
||||
}
|
||||
|
||||
*[Symbol.iterator]() {
|
||||
const { htmlParts, expressions } = this;
|
||||
|
||||
|
|
|
@ -1,23 +1,21 @@
|
|||
/**
|
||||
* UNCOMMENT: add markdown support
|
||||
import { expect } from 'chai';
|
||||
import cheerio from 'cheerio';
|
||||
import { loadFixture } from './test-utils.js';
|
||||
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
projectRoot: './fixtures/astro-markdown/',
|
||||
renderers: ['@astrojs/renderer-preact'],
|
||||
buildOptions: {
|
||||
sitemap: false,
|
||||
},
|
||||
});
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
describe('Astro Markdown', () => {
|
||||
let fixture;
|
||||
|
||||
before(async () => {
|
||||
fixture = await loadFixture({
|
||||
projectRoot: './fixtures/astro-markdown/',
|
||||
renderers: ['@astrojs/renderer-preact'],
|
||||
buildOptions: {
|
||||
sitemap: false,
|
||||
},
|
||||
});
|
||||
await fixture.build();
|
||||
});
|
||||
|
||||
it('Can load markdown pages with Astro', async () => {
|
||||
const html = await fixture.readFile('/post/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
@ -37,7 +35,7 @@ describe('Astro Markdown', () => {
|
|||
});
|
||||
|
||||
it('Empty code blocks do not fail', async () => {
|
||||
const html = await fixture.fetch('/empty-code/index.html');
|
||||
const html = await fixture.readFile('/empty-code/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// test 1: There is not a `<code>` in the codeblock
|
||||
|
@ -47,15 +45,17 @@ describe('Astro Markdown', () => {
|
|||
expect($('pre')[1].children).to.have.lengthOf(0);
|
||||
});
|
||||
|
||||
it('Runs code blocks through syntax highlighter', async () => {
|
||||
// This doesn't work because the markdown plugin doesn't have Prism support yet.
|
||||
it.skip('Runs code blocks through syntax highlighter', async () => {
|
||||
const html = await fixture.readFile('/code/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// test 1: There are child spans in code blocks
|
||||
expect($('code span').length).toBeGreaterThan(0);
|
||||
expect($('code span').length).greaterThan(0);
|
||||
});
|
||||
|
||||
it('Scoped styles should not break syntax highlight', async () => {
|
||||
// Blocked by lack of syntax highlighting
|
||||
it.skip('Scoped styles should not break syntax highlight', async () => {
|
||||
const html = await fixture.readFile('/scopedStyles-code/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
|
@ -80,28 +80,19 @@ describe('Astro Markdown', () => {
|
|||
expect($('#deep').children()).to.have.lengthOf(3);
|
||||
|
||||
// tests 2–4: Only rendered title in each section
|
||||
assert.equal($('.a').children()).to.have.lengthOf(1);
|
||||
assert.equal($('.b').children()).to.have.lengthOf(1);
|
||||
assert.equal($('.c').children()).to.have.lengthOf(1);
|
||||
expect($('.a').children()).to.have.lengthOf(1);
|
||||
expect($('.b').children()).to.have.lengthOf(1);
|
||||
expect($('.c').children()).to.have.lengthOf(1);
|
||||
|
||||
// test 5–7: Rendered title in correct section
|
||||
assert.equal($('.a > h2').text()).to.equal('A');
|
||||
assert.equal($('.b > h2').text()).to.equal('B');
|
||||
assert.equal($('.c > h2').text()).to.equal('C');
|
||||
expect($('.a > h2').text()).to.equal('A');
|
||||
expect($('.b > h2').text()).to.equal('B');
|
||||
expect($('.c > h2').text()).to.equal('C');
|
||||
});
|
||||
|
||||
it('Renders recursively', async () => {
|
||||
const html = await fixture.readFile('/recursive/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// tests 1–2: Rendered title correctly
|
||||
expect($('.a > h1').text()).to.equal('A');
|
||||
expect($('.b > h1').text()).to.equal('B');
|
||||
expect($('.c > h1').text()).to.equal('C');
|
||||
});
|
||||
|
||||
it('Renders dynamic content though the content attribute', async () => {
|
||||
it.skip('Renders dynamic content though the content attribute', async () => {
|
||||
const html = await fixture.readFile('/external/index.html');
|
||||
console.log(html)
|
||||
const $ = cheerio.load(html);
|
||||
|
||||
// test 1: Rendered markdown content
|
||||
|
@ -140,8 +131,8 @@ describe('Astro Markdown', () => {
|
|||
});
|
||||
|
||||
it('Can render markdown with --- for horizontal rule', async () => {
|
||||
const result = await fixture.readFile('/dash/index.html');
|
||||
expect(result.status).to.equal(200);
|
||||
const html = await fixture.readFile('/dash/index.html');
|
||||
expect(!!html).to.equal(true);
|
||||
});
|
||||
|
||||
it('Can render markdown content prop (#1259)', async () => {
|
||||
|
@ -151,8 +142,4 @@ describe('Astro Markdown', () => {
|
|||
// test Markdown rendered correctly via content prop
|
||||
expect($('h1').text()).to.equal('Foo');
|
||||
});
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
it.skip('is skipped', () => {});
|
||||
});
|
|
@ -8,13 +8,11 @@ const title = 'My Blog Post';
|
|||
const description = 'This is a post about some stuff.';
|
||||
---
|
||||
|
||||
<Markdown>
|
||||
<Layout>
|
||||
|
||||
<Layout>
|
||||
<Markdown>
|
||||
## Interesting Topic
|
||||
|
||||
<Hello name={`world`} />
|
||||
<Counter client:load />
|
||||
|
||||
</Layout>
|
||||
</Markdown>
|
||||
</Markdown>
|
||||
</Layout>
|
|
@ -1,15 +1,16 @@
|
|||
---
|
||||
import { Markdown } from 'astro/components';
|
||||
import Hello from '../components/Hello.jsx';
|
||||
|
||||
const outer = `# Outer`;
|
||||
const inner = `## Inner`;
|
||||
---
|
||||
|
||||
<Markdown content={outer} />
|
||||
<div>
|
||||
<Markdown content={outer} />
|
||||
|
||||
<Markdown>
|
||||
# Nested
|
||||
<Markdown>
|
||||
# Nested
|
||||
|
||||
<Markdown content={inner} />
|
||||
</Markdown>
|
||||
<Markdown content={inner} />
|
||||
</Markdown>
|
||||
</div>
|
|
@ -1,15 +0,0 @@
|
|||
---
|
||||
import { Markdown } from 'astro/components';
|
||||
---
|
||||
|
||||
<Markdown>
|
||||
<div class="a">
|
||||
# A
|
||||
<div class="b">
|
||||
# B
|
||||
<div class="c">
|
||||
# C
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Markdown>
|
|
@ -3,12 +3,12 @@ import { Markdown } from 'astro/components';
|
|||
import Layout from '../layouts/content.astro';
|
||||
|
||||
---
|
||||
<Markdown>
|
||||
<Layout>
|
||||
## Interesting Topic
|
||||
<Markdown>
|
||||
## Interesting Topic
|
||||
|
||||
```js
|
||||
const thing = () => {};
|
||||
```
|
||||
</Layout>
|
||||
</Markdown>
|
||||
```js
|
||||
const thing = () => {};
|
||||
```
|
||||
</Markdown>
|
||||
</Layout>
|
|
@ -1,44 +0,0 @@
|
|||
import { visit } from 'unist-util-visit';
|
||||
import type { Element, Root as HastRoot, Properties } from 'hast';
|
||||
import type { Root as MdastRoot } from 'mdast';
|
||||
|
||||
/** */
|
||||
export function remarkCodeBlock() {
|
||||
return function (tree: MdastRoot) {
|
||||
visit(tree, 'code', (node) => {
|
||||
const { data, meta } = node;
|
||||
let lang = node.lang || 'html'; // default to html to match GFM behavior.
|
||||
|
||||
let currentClassName = (data?.hProperties as Properties)?.class ?? '';
|
||||
node.data = node.data || {};
|
||||
node.data.hProperties = node.data.hProperties || {};
|
||||
node.data.hProperties = { ...(node.data.hProperties as Properties), class: `language-${lang} ${currentClassName}`.trim(), lang, meta };
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/** */
|
||||
export function rehypeCodeBlock() {
|
||||
return function (tree: HastRoot) {
|
||||
const escapeCode = (code: Element): void => {
|
||||
code.children = code.children.map((child) => {
|
||||
if (child.type === 'text') {
|
||||
return { ...child, value: `{\`${child.value.replace(/\$\{/g, '\\$\\{').replace(/`/g, '\\`')}\`}` };
|
||||
}
|
||||
return child;
|
||||
});
|
||||
};
|
||||
visit(tree, 'element', (node) => {
|
||||
if (node.tagName === 'code') {
|
||||
escapeCode(node);
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.tagName !== 'pre') return;
|
||||
if (!node.children[0]) return;
|
||||
const code = node.children[0];
|
||||
if (code.type !== 'element' || code.tagName !== 'code') return;
|
||||
node.properties = { ...code.properties };
|
||||
});
|
||||
};
|
||||
}
|
|
@ -6,8 +6,7 @@ import { remarkExpressions, loadRemarkExpressions } from './remark-expressions.j
|
|||
import rehypeExpressions from './rehype-expressions.js';
|
||||
import { remarkJsx, loadRemarkJsx } from './remark-jsx.js';
|
||||
import rehypeJsx from './rehype-jsx.js';
|
||||
import { remarkCodeBlock, rehypeCodeBlock } from './codeblock.js';
|
||||
import remarkSlug from './remark-slug.js';
|
||||
//import { remarkCodeBlock } from './codeblock.js';
|
||||
import { loadPlugins } from './load-plugins.js';
|
||||
|
||||
import { unified } from 'unified';
|
||||
|
@ -59,7 +58,7 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp
|
|||
// parser.use(scopedStyles(scopedClassName));
|
||||
// }
|
||||
|
||||
parser.use(remarkCodeBlock);
|
||||
//parser.use(remarkCodeBlock);
|
||||
parser.use(markdownToHtml, { allowDangerousHtml: true, passThrough: ['raw', 'mdxTextExpression', 'mdxJsxTextElement', 'mdxJsxFlowElement']});
|
||||
|
||||
loadedRehypePlugins.forEach(([plugin, opts]) => {
|
||||
|
@ -71,12 +70,13 @@ export async function renderMarkdown(content: string, opts?: MarkdownRenderingOp
|
|||
let result: string;
|
||||
try {
|
||||
const vfile = await parser
|
||||
.use(rehypeCollectHeaders)
|
||||
.use(rehypeCodeBlock)
|
||||
.use(rehypeCollectHeaders)
|
||||
.use(rehypeStringify, { allowParseErrors: true, preferUnquoted: true, allowDangerousHtml: true })
|
||||
.process(content);
|
||||
result = vfile.toString();
|
||||
} catch (err) {
|
||||
debugger;
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,11 +22,10 @@ export default function createCollectHeaders() {
|
|||
text += child.value;
|
||||
});
|
||||
|
||||
let slug = node?.data?.id || slugger.slug(text);
|
||||
let slug = node?.properties?.id || slugger.slug(text);
|
||||
|
||||
node.data = node.data || {};
|
||||
node.data.properties = node.data.properties || {};
|
||||
node.data.properties = { ...(node.data.properties as Properties), slug };
|
||||
node.properties = node.properties || {};
|
||||
node.properties.id = slug;
|
||||
headers.push({ depth, slug, text });
|
||||
});
|
||||
};
|
||||
|
|
|
@ -7,7 +7,7 @@ export function remarkJsx(this: any, options: any) {
|
|||
let settings = options || {};
|
||||
let data = this.data();
|
||||
|
||||
add('micromarkExtensions', mdxJsx({}));
|
||||
// TODO this seems to break adding slugs, no idea why add('micromarkExtensions', mdxJsx({}));
|
||||
add('fromMarkdownExtensions', mdxJsxFromMarkdown);
|
||||
add('toMarkdownExtensions', mdxJsxToMarkdown);
|
||||
|
||||
|
|
Loading…
Reference in a new issue