Skip to content

Commit

Permalink
feat: export frontmatter in one metadata object
Browse files Browse the repository at this point in the history
  • Loading branch information
nvlang committed Jul 18, 2024
1 parent 3756899 commit 2b81d16
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 100 deletions.
10 changes: 7 additions & 3 deletions src/utils/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,15 @@ export function handleFrontmatter(snippet: ProcessableSnippet<'frontmatter'>): {
const { title, base, noscript, link, meta, imports } = frontmatter;

Object.entries(frontmatter).forEach(([key, value]) => {
scriptModuleLines.push(
`export const ${key} = ${JSON.stringify(value)};`,
);
scriptModuleLines.push(`${key}: ${JSON.stringify(value)},`);
scriptLines.push(`const ${key} = ${JSON.stringify(value)};`);
});

if (scriptModuleLines.length > 0) {
scriptModuleLines.unshift('export const metadata = {');
scriptModuleLines.push('};');
}

// Imports
if (
imports &&
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/base/Sveltex/sveltex.script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('Sveltex', () => {
filename: '9ae17b43-d19c-4ca3-9772-36e506ffb4a5.sveltex',
});
expect((scriptModuleOut as Processed).code).toContain(
'export const foo = "bar";\nexport const author = "Jane Doe";\nexport const title = "Example";\nexport const meta = [{"name":"author","content":"Jane Doe"}];\n',
'export const metadata = {\nfoo: "bar",\nauthor: "Jane Doe",\ntitle: "Example",\nmeta: [{"name":"author","content":"Jane Doe"}],\n};',
);

existsSync.mockReset();
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('Sveltex', () => {
filename: 'a0dcf7dd-cabd-4816-a963-c30fc654ff34.sveltex',
});
expect((scriptOut as Processed).code).toMatch(
/^\s*import Example from '\$lib\/components\/Example.svelte';\s*$/,
/^\s*const imports = \[\{"\$lib\/components\/Example.svelte":"Example;"\}\];\n+import Example from '\$lib\/components\/Example.svelte';\s*$/,
);

const scriptModuleOut = await sp.script({
Expand All @@ -293,7 +293,7 @@ describe('Sveltex', () => {
filename: 'a0dcf7dd-cabd-4816-a963-c30fc654ff34.sveltex',
});
expect((scriptModuleOut as Processed).code).toContain(
'export const imports = [{"$lib/components/Example.svelte":"Example;"}];',
'export const metadata = {\nimports: [{"$lib/components/Example.svelte":"Example;"}],\n};',
);
existsSync.mockReset();
});
Expand Down
1 change: 0 additions & 1 deletion tests/unit/handlers/MathHandler/MathHandler.katex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
} from 'vitest';
import { MathHandler } from '$handlers/MathHandler.js';
import { spy } from '$tests/unit/fixtures.js';
import { v4 as uuid } from 'uuid';
import type { MathConfiguration } from '$types/handlers/Math.js';

function fixture() {
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/handlers/MathHandler/MathHandler.mathjax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
} from 'vitest';
import { MathHandler } from '$handlers/MathHandler.js';
import { spy } from '$tests/unit/fixtures.js';
import { v4 as uuid } from 'uuid';
import { consoles } from '$utils/debug.js';
import type { SupportedCdn } from '$types/handlers/Css.js';
import type { PossibleMathCssApproach } from '$types/handlers/Math.js';
import { sveltex } from '$base/Sveltex.js';
Expand Down
Loading

0 comments on commit 2b81d16

Please sign in to comment.