Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'preserve' rules. #479

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ export default function Rules (options) {
this.options = options
this._keep = []
this._remove = []
this._preserve = []

this.blankRule = {
replacement: options.blankReplacement
}

this.keepReplacement = options.keepReplacement
this.preserveReplacement = options.preserveReplacement

this.defaultRule = {
replacement: options.defaultReplacement
Expand Down Expand Up @@ -42,12 +44,20 @@ Rules.prototype = {
})
},

preserve: function (filter) {
this._preserve.unshift({
filter: filter,
replacement: this.preserveReplacement
})
},

forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule

if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._preserve, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

return this.defaultRule
Expand Down
24 changes: 23 additions & 1 deletion src/turndown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import COMMONMARK_RULES from './commonmark-rules'
import Rules from './rules'
import { extend, trimLeadingNewlines, trimTrailingNewlines } from './utilities'
import { extend, trimLeadingNewlines, trimTrailingNewlines, getOpenTag, getCloseTag } from './utilities'
import RootNode from './root-node'
import Node from './node'
var reduce = Array.prototype.reduce
Expand Down Expand Up @@ -44,6 +44,15 @@ export default function TurndownService (options) {
},
defaultReplacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
},
preserveReplacement: function (innerContent, node) {
const html = node.cloneNode()
html.setAttribute('markdown', '1')
const openTag = getOpenTag(html)
const closeTag = getCloseTag(html)

const outerContent = openTag + innerContent + closeTag
return node.isBlock ? '\n\n' + outerContent + '\n\n' : outerContent
}
}
this.options = extend({}, defaults, options)
Expand Down Expand Up @@ -131,6 +140,19 @@ TurndownService.prototype = {
return this
},

/**
* Preserves (using markdown="1") a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/

preserve: function (filter) {
this.rules.preserve(filter)
return this
},

/**
* Escapes Markdown syntax
* @public
Expand Down
17 changes: 17 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ function has (node, tagNames) {
})
)
}

export function getOpenTag (element) {
// Credit: https://stackoverflow.com/questions/9604235
const outerHtml = element.outerHTML
const len = outerHtml.length

if (outerHtml[len - 2] === '/') { // Is self-closing tag?
return outerHtml.slice(0, len - 2) + '>'
} else {
const openTagLength = len - element.innerHTML.length - (element.tagName.length + 3)
return outerHtml.slice(0, openTagLength)
}
}

export function getCloseTag (element) {
return `</${element.tagName.toLowerCase()}>`
}