
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
shortcode-tokenizer
Advanced tools
[](http://travis-ci.org/mblarsen/shortcode-tokenizer) [](https://covera
Tokenizes a string containing shortcodes (re-popularized by WordPress) and outputs it as an AST that can be used for further parsing.
If you are only looking for simple transformations from a shortcode to a string I suggest that you give these libs a try:
If you need more control this is the lib for you.
npm install shortcode-tokenizer
import ShortcodeTokenizer from 'shortcode-tokenizer'
const input = `
<h1>Cool Shop</h1>
[row]
[col width=6 class="featured"]
[product-list list="featured" /]
[/col]
[col width=6 class="featured"]
<div>Ad: Buy more, Buy often!</div>
[/col]
[/row]
`
const tokenizer = new ShortcodeTokenizer(input)
tokenizer.ast()
The AST outputted is at root level an array of two nodes, a text node and a code-node:
[
{
type: 'TEXT',
body: "<h1>Cool Shop</h1>"
pos: 0
},
{
type: 'OPEN',
name: 'row',
pos: 19,
body: '[row]',
isClosed: true,
params: {},
children: [
... a whitespace TEXT token ...,
{
type: 'OPEN',
name: 'col',
pos: 27,
body: '[col width=6 class="featured"]',
isClosed: true,
params: {
width: 6,
class: 'featured'
},
children: [ ... and so on ... ]
}
]
}
]
(Note: the same data-structure is used to represent tokens from the lexing and nodes in the AST, see CLOSE, below)
There are 5 token types:
body
contains the content.[row]
.[post id=1/]
.[/row]
. You will only see the left-over of these
tokens in the AST as OPEN tokens that have their isClosed
value set to
true.options.strict
By default strict-mode is enabled and syntax errors will be thrown. Setting strict-mode to false will convert all errors into ERROR nodes.
options.skipWhiteSpace
By default skipWhiteSpace is off. When turned on all whitespace is trimmed and if there is nothing left the TEXT node is skippid.
input()
You can pass input in the constructor you can set it later using input()
.
Examples:
let t = new ShortcodeTokenizer('[code][/code]')
t.ast()
t.input('[code][/code]')
t.ast()
t.ast('[code][/code]')
tokens()
Returns all tokens.
ast([input])
Returns an AST created from the input.
buildTemplate([Token token], [object|string|null params=null], [bool deep=false])
Builds template on given token. You can overwrite params in root param by params
and also in all nested setting deep="true"
param.
See CHANGELOG.md
FAQs
[](http://travis-ci.org/mblarsen/shortcode-tokenizer) [](https://covera
We found that shortcode-tokenizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.