Skip to content

Commit

Permalink
fix: pass raw frontmatter to when parsing markdown in glob loader (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Dec 19, 2024
1 parent 5e9d1bc commit f632b94
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fresh-gifts-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Pass raw frontmatter to remark plugins in glob loader
2 changes: 1 addition & 1 deletion packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function glob(globOptions: GlobOptions): Loader {
try {
rendered = await render?.({
id,
data: parsedData,
data,
body,
filePath,
digest,
Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/astro-markdown-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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/
Original file line number Diff line number Diff line change
@@ -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" }],
});
};
},
],
},
});
Original file line number Diff line number Diff line change
@@ -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:*"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Test Markdown
foo: bar
---

# Test Markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Test MDX
foo: bar
---

Hello from MDX!

[Markdown page](/test1)
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f632b94

Please sign in to comment.