Skip to content

Commit f632b94

Browse files
authored
fix: pass raw frontmatter to when parsing markdown in glob loader (#12789)
1 parent 5e9d1bc commit f632b94

File tree

13 files changed

+171
-1
lines changed

13 files changed

+171
-1
lines changed

.changeset/fresh-gifts-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Pass raw frontmatter to remark plugins in glob loader

packages/astro/src/content/loaders/glob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export function glob(globOptions: GlobOptions): Loader {
171171
try {
172172
rendered = await render?.({
173173
id,
174-
data: parsedData,
174+
data,
175175
body,
176176
filePath,
177177
digest,

packages/astro/test/astro-markdown-plugins.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ describe('Astro Markdown plugins', () => {
120120
testRemark(html);
121121
testRehype(html, '#smartypants-test');
122122
});
123+
124+
describe("content layer plugins", () => {
125+
let fixture
126+
before(async () => {
127+
fixture = await loadFixture({
128+
root: './fixtures/content-layer-remark-plugins/',
129+
});
130+
await fixture.build();
131+
});
132+
133+
it('passes untransformed frontmatter to remark plugins', async () => {
134+
const html = await fixture.readFile('/test1/index.html');
135+
const $ = cheerio.load(html);
136+
assert.equal($('p').text(), 'Not transformed');
137+
});
138+
139+
});
140+
123141
});
124142

125143
function testRehype(html, headingId) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# build output
2+
dist/
3+
# generated types
4+
.astro/
5+
6+
# dependencies
7+
node_modules/
8+
9+
# logs
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
pnpm-debug.log*
14+
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store
22+
23+
# jetbrains setting folder
24+
.idea/
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @ts-check
2+
import { defineConfig } from 'astro/config';
3+
import mdx from '@astrojs/mdx';
4+
5+
// https://astro.build/config
6+
export default defineConfig({
7+
integrations: [mdx()],
8+
markdown: {
9+
remarkPlugins: [
10+
function myRemarkPlugin() {
11+
return function transformer(tree, file) {
12+
tree.children.push({
13+
type: 'paragraph',
14+
children: [{ type: 'text', value: file?.data?.astro?.frontmatter?.addedByTransformer ? "Transformed" : "Not transformed" }],
15+
});
16+
};
17+
},
18+
],
19+
},
20+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@test/content-layer-remark-plugins",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"private": true,
6+
"scripts": {
7+
"dev": "astro dev",
8+
"build": "astro build",
9+
"preview": "astro preview",
10+
"astro": "astro"
11+
},
12+
"dependencies": {
13+
"@astrojs/mdx": "workspace:*",
14+
"astro": "workspace:*"
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineCollection, z } from 'astro:content';
2+
import { glob } from 'astro/loaders';
3+
4+
const docs = defineCollection({
5+
loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/docs' }),
6+
schema: z
7+
.object({
8+
title: z.string(),
9+
})
10+
.transform((data) => ({
11+
...data,
12+
someOtherField: 'Added by transform',
13+
})),
14+
});
15+
16+
export const collections = { docs };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
title: Test Markdown
3+
foo: bar
4+
---
5+
6+
# Test Markdown
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Test MDX
3+
foo: bar
4+
---
5+
6+
Hello from MDX!
7+
8+
[Markdown page](/test1)

0 commit comments

Comments
 (0)