From 6f708501c79e3b34bac5db3110bde7535fe218ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Metz?= Date: Mon, 6 May 2024 14:13:43 +0200 Subject: [PATCH] Add marked extension support. --- README.md | 5 ----- src/index.ts | 15 ++++++++------- test/markdown.test.js | 38 ++++++++++++++++++++++++++++++++++---- test/package.json | 1 + yarn.lock | 19 +++++++++++++++++++ 5 files changed, 62 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f35814d..cb7d380 100644 --- a/README.md +++ b/README.md @@ -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 } } ``` diff --git a/src/index.ts b/src/index.ts index e007777..3c161a1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -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'; @@ -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 }); diff --git a/test/markdown.test.js b/test/markdown.test.js index ae75fa5..f1fe7bb 100644 --- a/test/markdown.test.js +++ b/test/markdown.test.js @@ -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) { @@ -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', - `

Markdown

+ `

Markdown

Markdown content

` ]); }); 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([ @@ -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', + `

Markdown

+

Markdown content

` + ]); +}); + +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', + `

Markdown

+

Markdown content

` + ]); +}); diff --git a/test/package.json b/test/package.json index c210f52..240ccd3 100644 --- a/test/package.json +++ b/test/package.json @@ -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:^" } } diff --git a/yarn.lock b/yarn.lock index 2347232..0bf9fde 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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