Skip to content

Commit

Permalink
Add support for keep option
Browse files Browse the repository at this point in the history
Closes GH-15.
Closes GH-17.

Reviewed-by: Titus Wormer <[email protected]>
  • Loading branch information
samuelmeuli authored and wooorm committed Aug 8, 2019
1 parent f92f73b commit e395323
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 35 deletions.
83 changes: 50 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,62 @@

module.exports = strip

function strip() {
return one
}

// Expose modifiers for available node types.
// Node types not listed here are not changed (but their children are).
var map = {}

map.heading = paragraph
map.text = text
map.inlineCode = text
map.image = image
map.imageReference = image
map.break = lineBreak

map.blockquote = children
map.list = children
map.listItem = children
map.strong = children
map.emphasis = children
map.delete = children
map.link = children
map.linkReference = children

map.code = empty
map.horizontalRule = empty
map.thematicBreak = empty
map.html = empty
map.table = empty
map.tableCell = empty
map.definition = empty
map.yaml = empty
map.toml = empty
var map = {
heading: paragraph,
text: text,
inlineCode: text,
image: image,
imageReference: image,
break: lineBreak,

blockquote: children,
list: children,
listItem: children,
strong: children,
emphasis: children,
delete: children,
link: children,
linkReference: children,

code: empty,
horizontalRule: empty,
thematicBreak: empty,
html: empty,
table: empty,
tableCell: empty,
definition: empty,
yaml: empty,
toml: empty
}
var mapFiltered

function strip(options) {
var keep = (options || {}).keep || []

// Remove node types specified in `keep` from map
mapFiltered = Object.assign({}, map)
keep.forEach(function(nodeType) {
if (nodeType in mapFiltered) {
delete mapFiltered[nodeType]
} else {
throw new Error(
'Invalid `keep` option: No modifier is defined for node type `' +
nodeType +
'`'
)
}
})

return one
}

function one(node) {
var type = node && node.type

if (type in map) {
node = map[type](node)
if (type in mapFiltered) {
node = mapFiltered[type](node)
}

if ('length' in node) {
Expand Down
23 changes: 21 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ var remark = require('remark')
var u = require('unist-builder')
var strip = require('.')

function proc(value) {
function proc(value, options) {
return remark()
.use(strip)
.use(strip, options)
.processSync(value)
.toString()
.trimRight()
Expand Down Expand Up @@ -83,5 +83,24 @@ test('stripMarkdown()', function(t) {
'html (3)'
)

// "keep" option
t.equal(
proc('- **Hello**\n\n- World!', {keep: []}),
'Hello\n\nWorld!',
'empty array as "keep" option'
)
t.equal(
proc('- **Hello**\n\n- World!', {keep: ['list', 'listItem']}),
'- Hello\n\n- World!',
'keep lists'
)
t.throws(
function() {
proc('- **Hello**\n\n- World!', {keep: ['typo']})
},
/Error: Invalid `keep` option/,
'invalid "keep" option'
)

t.end()
})

0 comments on commit e395323

Please sign in to comment.