From a64b38efad521b48dba6aca8209748e509cebe6f Mon Sep 17 00:00:00 2001 From: Benjamin Canac Date: Mon, 25 May 2020 18:08:01 +0200 Subject: [PATCH] feat: support remark plugins --- lib/index.js | 12 ++++++++++++ lib/parsers/markdown/index.js | 28 ++++++++++++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index b7f1a6b20..ca2d522b0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -13,6 +13,18 @@ const defaults = { dir: 'content', fullTextSearchFields: ['title', 'description', 'slug', 'text'], markdown: { + basePlugins: [ + 'remark-squeeze-paragraphs', + 'remark-slug', + 'remark-autolink-headings', + 'remark-external-links', + 'remark-footnotes' + ], + plugins: [ + ], + footnotes: { + inlineNotes: true + }, externalLinks: {}, prism: { theme: 'prismjs/themes/prism.css' diff --git a/lib/parsers/markdown/index.js b/lib/parsers/markdown/index.js index cc3f8f238..22784addb 100644 --- a/lib/parsers/markdown/index.js +++ b/lib/parsers/markdown/index.js @@ -1,16 +1,13 @@ const matter = require('gray-matter') +const { camelCase } = require('change-case') const unified = require('unified') const parse = require('remark-parse') -const squeezeParagraphs = require('remark-squeeze-paragraphs') -const slug = require('remark-slug') -const headings = require('remark-autolink-headings') -const externalLinks = require('remark-external-links') -const footnotes = require('remark-footnotes') const remark2rehype = require('remark-rehype') const minifyWhiteSpace = require('rehype-minify-whitespace') const sortValues = require('rehype-sort-attribute-values') const sortAttrs = require('rehype-sort-attributes') const raw = require('rehype-raw') +const logger = require('consola').withScope('@nuxt/content') const handlers = require('./handlers') const jsonCompiler = require('./compilers/json') @@ -18,6 +15,7 @@ const jsonCompiler = require('./compilers/json') class Markdown { constructor (options = {}) { this.options = options + this.plugins = [...options.basePlugins, ...options.plugins] } /** @@ -64,13 +62,19 @@ class Markdown { */ generateBody (content) { return new Promise((resolve, reject) => { - unified() - .use(parse) - .use(slug) - .use(headings) - .use(squeezeParagraphs) - .use(externalLinks, this.options.externalLinks) - .use(footnotes, { inlineNotes: true }) + let stream = unified().use(parse) + + for (const name of this.plugins) { + try { + const plugin = require(name) + const options = this.options[camelCase(name.replace('remark-', ''))] + stream = stream.use(plugin, options) + } catch (e) { + logger.warn(`${name} is not installed`) + } + } + + stream .use(remark2rehype, { handlers, allowDangerousHtml: true }) .use(raw) .use(minifyWhiteSpace)