Skip to content

Commit

Permalink
Add marked extension support.
Browse files Browse the repository at this point in the history
  • Loading branch information
francois2metz committed May 6, 2024
1 parent bbfe950 commit 6f70850
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ There is a `marked` configuration that converts `markdown` to `HTML`. Otherwise
"breaks": true,
"pedantic": false,
"gfm": true,
"tables": true,
"sanitize": false,
"smartLists": true,
"smartypants": false,
"xhtml": false
}
}
```
Expand Down
15 changes: 8 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { Transformer } from '@parcel/plugin';
import { marked, type MarkedOptions } from 'marked';
import { marked, type MarkedOptions, type MarkedExtension } from 'marked';
import yamlFrontmatter from 'yaml-front-matter';
const { loadFront } = yamlFrontmatter;

Expand All @@ -17,12 +17,10 @@ export default new Transformer({
breaks: true,
pedantic: false,
gfm: true,
tables: true,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
headerIds: false,
mangle: false
},
extensions: [],
};
if (conf) {
let isJavascript = path.extname(conf.filePath) === '.js';
Expand All @@ -40,7 +38,10 @@ export default new Transformer({
async transform({ asset, config }) {
const code = await asset.getCode();
const frontMatter = loadFront(code);
const option: { marked?: MarkedOptions } = config || {};
const option: { marked?: MarkedOptions, extensions?: MarkedExtension[] } = config || {};
option.extensions?.forEach((extension) => {
marked.use(extension);
})
const result = { ...frontMatter };
if (option.marked) {
result.__content = marked.parse(frontMatter.__content, { ...option.marked });
Expand Down
38 changes: 34 additions & 4 deletions test/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ async function copySiteToTempDir() {
return dir;
}

function writeMarkedConfig(dir, config) {
return fs.writeFile(path.join(dir, '.markedrc'), JSON.stringify(config));
function writeMarkedConfig(dir, file, content) {
return fs.writeFile(path.join(dir, file), content);
}

function renderSite(dir) {
Expand Down Expand Up @@ -76,14 +76,14 @@ test('render a markdown file to HTML', async () => {
const logs = await runAndGetConsoleOutput(path.join(dir, 'dist'));
expect(logs).toEqual([
'parcel-transformer-markdown-front-matter',
`<h1 id="markdown">Markdown</h1>
`<h1>Markdown</h1>
<p>Markdown content</p>`
]);
});

test('render a markdown file without HTML', async () => {
const dir = await copySiteToTempDir();
await writeMarkedConfig(dir, { marked: false });
await writeMarkedConfig(dir, '.markedrc', JSON.stringify({ marked: false }));
await renderSite(dir);
const logs = await runAndGetConsoleOutput(path.join(dir, 'dist'));
expect(logs).toEqual([
Expand All @@ -93,3 +93,33 @@ test('render a markdown file without HTML', async () => {
Markdown content`
]);
});

test('render a markdown file with marked extension: marked-gfm-heading-id', async () => {
const dir = await copySiteToTempDir();
await writeMarkedConfig(dir, '.markedrc.js', `const { gfmHeadingId } = require('marked-gfm-heading-id');
module.exports = {
extensions: [gfmHeadingId()]
};`);
await renderSite(dir);
const logs = await runAndGetConsoleOutput(path.join(dir, 'dist'));
expect(logs).toEqual([
'parcel-transformer-markdown-front-matter',
`<h1 id="markdown">Markdown</h1>
<p>Markdown content</p>`
]);
});

test('render a markdown file with marked extension and options: marked-gfm-heading-id', async () => {
const dir = await copySiteToTempDir();
await writeMarkedConfig(dir, '.markedrc.js', `const { gfmHeadingId } = require('marked-gfm-heading-id');
module.exports = {
extensions: [gfmHeadingId({ prefix: 'test-' })]
};`);
await renderSite(dir);
const logs = await runAndGetConsoleOutput(path.join(dir, 'dist'));
expect(logs).toEqual([
'parcel-transformer-markdown-front-matter',
`<h1 id="test-markdown">Markdown</h1>
<p>Markdown content</p>`
]);
});
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@parcel/core": "^2.12.0",
"jest": "^29.7.0",
"jsdom": "^24.0.0",
"marked-gfm-heading-id": "^3.1.0",
"parcel-transformer-markdown-front-matter": "workspace:^"
}
}
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3056,6 +3056,13 @@ __metadata:
languageName: node
linkType: hard

"github-slugger@npm:^2.0.0":
version: 2.0.0
resolution: "github-slugger@npm:2.0.0"
checksum: 10c0/21b912b6b1e48f1e5a50b2292b48df0ff6abeeb0691b161b3d93d84f4ae6b1acd6ae23702e914af7ea5d441c096453cf0f621b72d57893946618d21dd1a1c486
languageName: node
linkType: hard

"glob@npm:^10.2.2, glob@npm:^10.3.10":
version: 10.3.12
resolution: "glob@npm:10.3.12"
Expand Down Expand Up @@ -4238,6 +4245,17 @@ __metadata:
languageName: node
linkType: hard

"marked-gfm-heading-id@npm:^3.1.0":
version: 3.1.3
resolution: "marked-gfm-heading-id@npm:3.1.3"
dependencies:
github-slugger: "npm:^2.0.0"
peerDependencies:
marked: ">=4 <13"
checksum: 10c0/675ea0847eb35b37f0b0b79270a511954afbca2a94b2b2d3b1dfd04f13e4bd18e8989dd1e4e82b407b2166e06d9af6aaf83ef2ba46b4c1fc665a06c83dd31469
languageName: node
linkType: hard

"marked@npm:^6.0.0":
version: 6.0.0
resolution: "marked@npm:6.0.0"
Expand Down Expand Up @@ -5378,6 +5396,7 @@ __metadata:
"@parcel/core": "npm:^2.12.0"
jest: "npm:^29.7.0"
jsdom: "npm:^24.0.0"
marked-gfm-heading-id: "npm:^3.1.0"
parcel-transformer-markdown-front-matter: "workspace:^"
languageName: unknown
linkType: soft
Expand Down

0 comments on commit 6f70850

Please sign in to comment.