Skip to content

Commit

Permalink
feat: support remark plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed May 25, 2020
1 parent 6c5bbeb commit a64b38e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
12 changes: 12 additions & 0 deletions lib/index.js
Expand Up @@ -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'
Expand Down
28 changes: 16 additions & 12 deletions lib/parsers/markdown/index.js
@@ -1,23 +1,21 @@
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')

class Markdown {
constructor (options = {}) {
this.options = options
this.plugins = [...options.basePlugins, ...options.plugins]
}

/**
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a64b38e

Please sign in to comment.