Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Jan 31, 2024
1 parent cd8b0d3 commit d3a6803
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 45 deletions.
131 changes: 123 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
"dependencies": {
"dlv": "^1.1.3",
"dset": "^3.1.2",
"marked": "^4.3.0"
"marked": "^11.2.0",
"marked-gfm-heading-id": "^3.1.2",
"marked-highlight": "^2.1.0",
"marked-mangle": "^1.1.6"
},
"devDependencies": {
"@types/markdown-it": "^13.0.7",
Expand All @@ -64,6 +67,8 @@
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-n": "^16.6.2",
"markdown-it": "^14.0.0",
"marked-smartypants": "^1.1.5",
"marked-smartypants-lite": "^1.0.2",
"metalsmith": "^2.6.2",
"microbundle": "^0.15.1",
"mocha": "^10.2.0",
Expand Down
30 changes: 24 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import { basename, dirname, extname, join } from 'path'

import { Marked } from 'marked'
import { gfmHeadingId } from 'marked-gfm-heading-id'
import { mangle } from 'marked-mangle'

import get from 'dlv'
import { dset as set } from 'dset'
import { marked } from 'marked'
import expandWildcardKeypaths from './expand-wildcard-keypath.js'

function defaultRender(source, options) {
return marked(source, options)
}

function refsObjectToMarkdown(refsObject) {
return Object.entries(refsObject)
.map(([refname, value]) => `[${refname}]: ${value}`)
.join('\n')
}

let markedInstance

/**
* @callback Render
* @param {string} source
* @param {Object} engineOptions
* @param {{ path: string, key: string}} context
*/
function defaultRender(source, options) {
return markedInstance.parse(source, options)
}

/**
* @typedef Options
Expand All @@ -36,7 +41,13 @@ const defaultOptions = {
keys: {},
wildcard: false,
render: defaultRender,
engineOptions: {},
engineOptions: {
use: [
// pre-marked 5.x these were included by default
gfmHeadingId(),
mangle()
]
},
globalRefs: {}
}

Expand All @@ -56,6 +67,13 @@ function markdown(options = defaultOptions) {
options.keys = { files: options.keys }
}

if (options.render === defaultOptions.render) {
const { use, ...engineOptions } = options.engineOptions
// extensions can be an array of MarkedExtension or an object of { extension-name: args }
markedInstance = new Marked(engineOptions)
if (use) use.forEach((ext) => markedInstance.use(ext))
}

return function markdown(files, metalsmith, done) {
const debug = metalsmith.debug('@metalsmith/markdown')
const matches = metalsmith.match('**/*.{md,markdown}', Object.keys(files))
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/keys/build/index.html

This file was deleted.

42 changes: 14 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import equal from 'assert-dir-equal'
import Metalsmith from 'metalsmith'
import { markedSmartypantsLite } from 'marked-smartypants-lite'
import markdownIt from 'markdown-it'
import markdown from '../src/index.js'
import { gfmHeadingId } from 'marked-gfm-heading-id'

const __dirname = dirname(fileURLToPath(import.meta.url))
const { name } = JSON.parse(readFileSync(resolve(__dirname, '../package.json'), 'utf-8'))
Expand Down Expand Up @@ -68,7 +70,7 @@ describe('@metalsmith/markdown', function () {
}),
new Promise((resolve) => {
const files = getFiles()
markdown({ smartypants: true })(files, msCommon(__dirname), () => {
markdown({ engineOptions: { use: [markedSmartypantsLite()] } })(files, msCommon(__dirname), () => {
resolve(files)
})
})
Expand All @@ -83,20 +85,11 @@ describe('@metalsmith/markdown', function () {
})
})

it('should convert markdown files', function (done) {
msCommon('test/fixtures/basic')
.use(
markdown({
engineOptions: {
smartypants: true
}
})
)
.build(function (err) {
if (err) return done(err)
equal('test/fixtures/basic/expected', 'test/fixtures/basic/build')
done()
})
it('should convert markdown files', async function () {
await msCommon('test/fixtures/basic')
.use(markdown({ engineOptions: { use: [markedSmartypantsLite(), gfmHeadingId()] } }))
.build()
equal('test/fixtures/basic/build', 'test/fixtures/basic/expected')
})

it('should skip non-markdown files', function (done) {
Expand Down Expand Up @@ -231,21 +224,16 @@ describe('@metalsmith/markdown', function () {
})
})

it('should parse nested key paths', function (done) {
msCommon('test/fixtures/nested-keys')
it('should parse nested key paths', async function () {
const files = await msCommon('test/fixtures/nested-keys')
.use(
markdown({
keys: ['custom', 'nested.key.path'],
engineOptions: {
smartypants: true
}
engineOptions: { use: [markedSmartypantsLite(), gfmHeadingId()] }
})
)
.build(function (err, files) {
if (err) return done(err)
assert.equal('<h1 id="hello">Hello</h1>\n', files['index.html'].nested.key.path)
done()
})
.build()
assert.equal(files['index.html'].nested.key.path, '<h1 id="hello">Hello</h1>\n')
})

it('should log a warning when a key is not renderable (= not a string)', (done) => {
Expand Down Expand Up @@ -381,9 +369,7 @@ describe('@metalsmith/markdown', function () {
markdown({
keys: ['arr.*', 'objarr.*.prop', 'wildcard.faq.*.*', 'wildcard.titles.*'],
wildcard: '*',
engineOptions: {
smartypants: true
}
engineOptions: { use: [markedSmartypantsLite(), gfmHeadingId()] }
})
)
.build(function (err, files) {
Expand Down

0 comments on commit d3a6803

Please sign in to comment.