shortcode-tree
Advanced tools
Comparing version 1.4.0 to 1.4.1
@@ -26,2 +26,3 @@ 'use strict'; | ||
/** | ||
@@ -71,5 +72,33 @@ * Traverses a tree node: extracts short codes from the node text, and traverses its child nodes. | ||
}, | ||
extractTextContent(input) { | ||
extractTextContent: function extractTextContent(input) { | ||
var treeRootNode = this.parse(input); | ||
var textContent = ""; | ||
var fnTraverseNodeForContent = function fnTraverseNodeForContent(node) { | ||
var appendText = null; | ||
if (node instanceof TextNode) { | ||
appendText = node.text; | ||
} else if (node instanceof ShortcodeNode) { | ||
if (node.children.length) { | ||
node.children.forEach(function (node) { | ||
fnTraverseNodeForContent(node); | ||
}); | ||
} else { | ||
// Topmost node with no children | ||
appendText = node.text; | ||
} | ||
} | ||
if (appendText && appendText.length) { | ||
if (textContent.length && !textContent.endsWith(' ')) { | ||
textContent += ' '; | ||
} | ||
textContent += appendText; | ||
} | ||
}; | ||
fnTraverseNodeForContent(treeRootNode); | ||
return textContent; | ||
} | ||
@@ -76,0 +105,0 @@ }; |
{ | ||
"name": "shortcode-tree", | ||
"version": "1.4.0", | ||
"version": "1.4.1", | ||
"description": "Parser library for reading short codes (BB codes) into a tree structure", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -33,2 +33,3 @@ # shortcode-tree | ||
- Stringify `Shortcode` objects to shortcode text | ||
- Extract only text / HTML from a document containg shortcodes | ||
@@ -188,2 +189,11 @@ ## Installation | ||
The `extractShortcodes` method returns an array of `Shortcode` objects. | ||
The `extractShortcodes` method returns an array of `Shortcode` objects. | ||
### Extracting text/HTML from a shortcode fragment | ||
var treeParser = require('shortcode-tree').ShortcodeTree; | ||
var input = `root [row][column]deep[/column][/row]`; | ||
treeParser.extractTextContent(input); | ||
// returns: "root deep" |
66118
1169
197