Skip to content

Commit

Permalink
Rename converters to rules
Browse files Browse the repository at this point in the history
  • Loading branch information
domchristie committed Oct 20, 2017
1 parent 889b8cb commit 7177b63
Show file tree
Hide file tree
Showing 3 changed files with 915 additions and 322 deletions.
34 changes: 17 additions & 17 deletions src/commonmark-converters.js → src/commonmark-rules.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { repeat } from './utilities'

var converters = {}
var rules = {}

converters.paragraph = {
rules.paragraph = {
filter: 'p',

replacement: function (content) {
return '\n\n' + content + '\n\n'
}
}

converters.lineBreak = {
rules.lineBreak = {
filter: 'br',

replacement: function (content, node, options) {
return options.br + '\n'
}
}

converters.heading = {
rules.heading = {
filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],

replacement: function (content, node, options) {
Expand All @@ -35,7 +35,7 @@ converters.heading = {
}
}

converters.blockquote = {
rules.blockquote = {
filter: 'blockquote',

replacement: function (content) {
Expand All @@ -45,7 +45,7 @@ converters.blockquote = {
}
}

converters.list = {
rules.list = {
filter: ['ul', 'ol'],

replacement: function (content, node) {
Expand All @@ -58,7 +58,7 @@ converters.list = {
}
}

converters.listItem = {
rules.listItem = {
filter: 'li',

replacement: function (content, node, options) {
Expand All @@ -79,7 +79,7 @@ converters.listItem = {
}
}

converters.indentedCodeBlock = {
rules.indentedCodeBlock = {
filter: function (node, options) {
return (
options.codeBlockStyle === 'indented' &&
Expand All @@ -98,7 +98,7 @@ converters.indentedCodeBlock = {
}
}

converters.fencedCodeBlock = {
rules.fencedCodeBlock = {
filter: function (node, options) {
return (
options.codeBlockStyle === 'fenced' &&
Expand All @@ -120,15 +120,15 @@ converters.fencedCodeBlock = {
}
}

converters.horizontalRule = {
rules.horizontalRule = {
filter: 'hr',

replacement: function (content, node, options) {
return '\n\n' + options.hr + '\n\n'
}
}

converters.inlineLink = {
rules.inlineLink = {
filter: function (node, options) {
return (
options.linkStyle === 'inlined' &&
Expand All @@ -144,7 +144,7 @@ converters.inlineLink = {
}
}

converters.referenceLink = {
rules.referenceLink = {
filter: function (node, options) {
return (
options.linkStyle === 'referenced' &&
Expand Down Expand Up @@ -190,23 +190,23 @@ converters.referenceLink = {
}
}

converters.emphasis = {
rules.emphasis = {
filter: ['em', 'i'],

replacement: function (content, node, options) {
return options.emDelimiter + content + options.emDelimiter
}
}

converters.strong = {
rules.strong = {
filter: ['strong', 'b'],

replacement: function (content, node, options) {
return options.strongDelimiter + content + options.strongDelimiter
}
}

converters.code = {
rules.code = {
filter: function (node) {
var hasSiblings = node.previousSibling || node.nextSibling
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings
Expand All @@ -220,7 +220,7 @@ converters.code = {
}
}

converters.image = {
rules.image = {
filter: 'img',

replacement: function (content, node) {
Expand All @@ -232,4 +232,4 @@ converters.image = {
}
}

export default converters
export default rules
32 changes: 16 additions & 16 deletions src/turndown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import COMMONMARK_CONVERTERS from './commonmark-converters'
import COMMONMARK_RULES from './commonmark-rules'
import OptionsValidator from './options-validator'
import { extend } from './utilities'
import RootNode from './root-node'
Expand All @@ -12,7 +12,7 @@ export default function TurndownService (options) {
if (!(this instanceof TurndownService)) return new TurndownService(options)

var defaults = {
converters: COMMONMARK_CONVERTERS,
rules: COMMONMARK_RULES,
headingStyle: 'setext',
hr: '* * *',
bulletListMarker: '*',
Expand Down Expand Up @@ -139,22 +139,22 @@ TurndownService.prototype = {
*/

replacementForNode: function replacementForNode (node) {
var converter = this.converterForNode(node)
var rule = this.ruleForNode(node)
var content = this.process(node)
var whitespace = node.flankingWhitespace
if (whitespace.leading || whitespace.trailing) content = content.trim()
return (
whitespace.leading +
converter.replacement(content, node, this.options) +
rule.replacement(content, node, this.options) +
whitespace.trailing
)
},

/**
* Finds a converter for a given node
* Finds a rule for a given node
*/

converterForNode: function converterForNode (node) {
ruleForNode: function ruleForNode (node) {
if (this.filterValue(this.options.keepConverter, node)) {
return this.options.keepConverter
}
Expand All @@ -165,32 +165,32 @@ TurndownService.prototype = {

if (node.isBlank) return { replacement: this.options.blankReplacement }

for (var key in this.options.converters) {
var converter = this.options.converters[key]
if (this.filterValue(converter, node)) return converter
for (var key in this.options.rules) {
var rule = this.options.rules[key]
if (this.filterValue(rule, node)) return rule
}

return { replacement: this.options.defaultReplacement }
},

filterValue: function filterValue (converter, node) {
var filter = converter.filter
filterValue: function filterValue (rule, node) {
var filter = rule.filter
if (typeof filter === 'string') {
if (filter === node.nodeName.toLowerCase()) return true
} else if (Array.isArray(filter)) {
if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true
} else if (typeof filter === 'function') {
if (filter.call(converter, node, this.options)) return true
if (filter.call(rule, node, this.options)) return true
} else {
throw new TypeError('`filter` needs to be a string, array, or function')
}
},

postProcess: function postProcess (output) {
for (var key in this.options.converters) {
var converter = this.options.converters[key]
if (typeof converter.append === 'function') {
output = join(output, converter.append(this.options))
for (var key in this.options.rules) {
var rule = this.options.rules[key]
if (typeof rule.append === 'function') {
output = join(output, rule.append(this.options))
}
}
return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '')
Expand Down
Loading

0 comments on commit 7177b63

Please sign in to comment.