Skip to content

Commit

Permalink
Replace metalsmith-tagcleaner with a custom marked renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
domoscargin committed Jul 27, 2022
1 parent 400cd62 commit d33e4d5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
13 changes: 13 additions & 0 deletions lib/marked-renderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// A custom renderer that avoids wrapping <img> tags in <p> tags
const { marked } = require('marked')

const markedRenderer = new marked.Renderer()

markedRenderer.paragraph = function (text) {
if (text.startsWith('<img')) {
return `${text}\n`
}
return `<p>${text}</p>\n`
}

module.exports = markedRenderer
6 changes: 2 additions & 4 deletions lib/metalsmith.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const metalsmith = require('metalsmith') // static site generator
const assets = require('metalsmith-assets') // copy static assets
const brokenLinkChecker = require('metalsmith-broken-link-checker')
const titleChecker = require('./metalsmith-title-checker.js')
const markedRenderer = require('./marked-renderer.js')
const env = require('metalsmith-env') // environment vars plugin
const hashAssets = require('metalsmith-fingerprint-ignore') // add hash to specified files and ignores files that match a pattern
const inplace = require('@metalsmith/in-place') // render templating syntax in your source files
const layouts = require('@metalsmith/layouts') // apply layouts to source files
const tagcleaner = require('metalsmith-tagcleaner') // Use tag cleaner to remove <p> tags around images
const permalinks = require('@metalsmith/permalinks') // apply a permalink pattern to files
const canonical = require('metalsmith-canonical') // add a canonical url property to pages

Expand Down Expand Up @@ -233,13 +233,11 @@ module.exports = metalsmith(__dirname) // __dirname defined by node.js: name of
gfm: true,
tables: true,
pedantic: true,
renderer: markedRenderer,
highlight: highlighter
}
}))

// use tag cleaner to remove <p> tags around images
.use(tagcleaner())

// apply a permalink pattern to files
.use(permalinks({
relative: false
Expand Down
59 changes: 19 additions & 40 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"metalsmith-env": "^2.2.0",
"metalsmith-fingerprint-ignore": "^2.0.0",
"metalsmith-rollup": "^2.0.0",
"metalsmith-tagcleaner": "0.0.2",
"metalsmith-uglify": "^2.4.1",
"nunjucks": "^3.2.2",
"puppeteer": "^1.14.0",
Expand Down

0 comments on commit d33e4d5

Please sign in to comment.