Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix

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.

Loading