0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix: pass raw frontmatter to when parsing markdown in glob loader (#12789)

This commit is contained in:
Matt Kane 2024-12-19 16:49:01 +00:00 committed by GitHub
parent 5e9d1bcf80
commit f632b94527
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 171 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Pass raw frontmatter to remark plugins in glob loader

View file

@ -171,7 +171,7 @@ export function glob(globOptions: GlobOptions): Loader {
try {
rendered = await render?.({
id,
data: parsedData,
data,
body,
filePath,
digest,

View file

@ -120,6 +120,24 @@ describe('Astro Markdown plugins', () => {
testRemark(html);
testRehype(html, '#smartypants-test');
});
describe("content layer plugins", () => {
let fixture
before(async () => {
fixture = await loadFixture({
root: './fixtures/content-layer-remark-plugins/',
});
await fixture.build();
});
it('passes untransformed frontmatter to remark plugins', async () => {
const html = await fixture.readFile('/test1/index.html');
const $ = cheerio.load(html);
assert.equal($('p').text(), 'Not transformed');
});
});
});
function testRehype(html, headingId) {

View file

@ -0,0 +1,24 @@
# build output
dist/
# generated types
.astro/
# dependencies
node_modules/
# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# environment variables
.env
.env.production
# macOS-specific files
.DS_Store
# jetbrains setting folder
.idea/

View file

@ -0,0 +1,20 @@
// @ts-check
import { defineConfig } from 'astro/config';
import mdx from '@astrojs/mdx';
// https://astro.build/config
export default defineConfig({
integrations: [mdx()],
markdown: {
remarkPlugins: [
function myRemarkPlugin() {
return function transformer(tree, file) {
tree.children.push({
type: 'paragraph',
children: [{ type: 'text', value: file?.data?.astro?.frontmatter?.addedByTransformer ? "Transformed" : "Not transformed" }],
});
};
},
],
},
});

View file

@ -0,0 +1,16 @@
{
"name": "@test/content-layer-remark-plugins",
"type": "module",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/mdx": "workspace:*",
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 128 128">
<path d="M50.4 78.5a75.1 75.1 0 0 0-28.5 6.9l24.2-65.7c.7-2 1.9-3.2 3.4-3.2h29c1.5 0 2.7 1.2 3.4 3.2l24.2 65.7s-11.6-7-28.5-7L67 45.5c-.4-1.7-1.6-2.8-2.9-2.8-1.3 0-2.5 1.1-2.9 2.7L50.4 78.5Zm-1.1 28.2Zm-4.2-20.2c-2 6.6-.6 15.8 4.2 20.2a17.5 17.5 0 0 1 .2-.7 5.5 5.5 0 0 1 5.7-4.5c2.8.1 4.3 1.5 4.7 4.7.2 1.1.2 2.3.2 3.5v.4c0 2.7.7 5.2 2.2 7.4a13 13 0 0 0 5.7 4.9v-.3l-.2-.3c-1.8-5.6-.5-9.5 4.4-12.8l1.5-1a73 73 0 0 0 3.2-2.2 16 16 0 0 0 6.8-11.4c.3-2 .1-4-.6-6l-.8.6-1.6 1a37 37 0 0 1-22.4 2.7c-5-.7-9.7-2-13.2-6.2Z" />
<style>
path { fill: #000; }
@media (prefers-color-scheme: dark) {
path { fill: #FFF; }
}
</style>
</svg>

After

Width:  |  Height:  |  Size: 749 B

View file

@ -0,0 +1,16 @@
import { defineCollection, z } from 'astro:content';
import { glob } from 'astro/loaders';
const docs = defineCollection({
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/docs' }),
schema: z
.object({
title: z.string(),
})
.transform((data) => ({
...data,
someOtherField: 'Added by transform',
})),
});
export const collections = { docs };

View file

@ -0,0 +1,6 @@
---
title: Test Markdown
foo: bar
---
# Test Markdown

View file

@ -0,0 +1,8 @@
---
title: Test MDX
foo: bar
---
Hello from MDX!
[Markdown page](/test1)

View file

@ -0,0 +1,27 @@
---
import { getCollection, render } from 'astro:content';
export async function getStaticPaths() {
const docs = await getCollection('docs');
return docs.map(doc => ({
params: { slug: doc.id },
props: { doc },
}));
}
const { doc } = Astro.props;
const { Content } = await render(doc);
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>{doc.data.title}</h1>
<Content />
</body>
</html>

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p><a href="/test1">Markdown page</a></p>
<p><a href="/test2">MDX page</a></p>
</body>
</html>

View file

@ -2655,6 +2655,15 @@ importers:
specifier: ^10.25.0
version: 10.25.1
packages/astro/test/fixtures/content-layer-remark-plugins:
dependencies:
'@astrojs/mdx':
specifier: workspace:*
version: link:../../../../integrations/mdx
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/content-layer-rendering:
dependencies:
'@astrojs/mdx':