Skip to content

Commit

Permalink
fix: Fixed an issue where the optional style specification was ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
akabekobeko committed Jun 28, 2021
1 parent d7c9b73 commit 88bdb6c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,20 @@ export interface Hooks {
}

/**
* Check the metadata with options.
* Check and update metadata with options.
* @param metadata Metadata.
* @param options Options.
* @returns Checked metadata.
*/
const checkMetadata = (
metadata: Metadata,
options: StringifyMarkdownOptions,
) => {
const result = { ...metadata };

if (metadata.title === undefined && options.title !== undefined) {
result.title = options.title;
metadata.title = options.title;
}

if (metadata.lang === undefined && options.language !== undefined) {
result.lang = options.language;
metadata.lang = options.language;
}

if (options.style) {
Expand All @@ -77,8 +74,6 @@ const checkMetadata = (
}
}
}

return result;
};

/**
Expand All @@ -99,9 +94,9 @@ export function VFM(
}: StringifyMarkdownOptions = {},
metadata: Metadata = {},
): Processor {
const meta = checkMetadata(metadata, { style, title, language });
if (meta.vfm && meta.vfm.math !== undefined) {
math = meta.vfm.math;
checkMetadata(metadata, { style, title, language });
if (metadata.vfm && metadata.vfm.math !== undefined) {
math = metadata.vfm.math;
}

const processor = unified()
Expand All @@ -114,7 +109,7 @@ export function VFM(
}

if (!partial) {
processor.use(doc, meta);
processor.use(doc, metadata);
}

processor.use(rehypeStringify);
Expand Down
39 changes: 36 additions & 3 deletions tests/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,40 @@ link:
expect(received).toBe(expected);
});

it('Styles from options', () => {
it('Styles from options (String)', () => {
const md = ``;
const received = stringify(md, { style: 'sample.css' });
const expected = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="sample.css">
</head>
<body></body>
</html>
`;
expect(received).toBe(expected);
});

it('Styles from options (String[])', () => {
const md = ``;
const received = stringify(md, { style: ['sample1.css', 'sample2.css'] });
const expected = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="sample1.css">
<link rel="stylesheet" href="sample2.css">
</head>
<body></body>
</html>
`;
expect(received).toBe(expected);
});

it('Styles from options with frontmatter', () => {
const md = `---
link:
- rel: 'stylesheet'
Expand Down Expand Up @@ -257,7 +290,7 @@ it('Do not convert date and time', () => {
date: 2021-06-27
date2: "2021-06-27"
---`;
const received = stringify(md, { style: ['sample2.css', 'sample3.css'] });
const received = stringify(md);
const expected = `<!doctype html>
<html>
<head>
Expand All @@ -284,7 +317,7 @@ head: |
<meta name="sample-meta1" content="meta1">
<meta name="sample-meta2" content="meta2">
---`;
const received = stringify(md, { style: ['sample2.css', 'sample3.css'] });
const received = stringify(md);
const expected = `<!doctype html>
<html>
<head>
Expand Down

0 comments on commit 88bdb6c

Please sign in to comment.