🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

parse-blocks

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parse-blocks

Parse basic template blocks in a string, like Handlebars block expressions but returns an object. Delimiters are customizable.

latest
Source
npmnpm
Version
0.2.1
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

parse-blocks NPM version

Parse basic template blocks in a string, like Handlebars block expressions but returns an object. Delimiters are customizable.

Install with npm

npm i parse-blocks --save

Run tests

npm test

Usage

var blocks = require('parse-blocks');

Example

Given the following arbitrary blocks in fixtures/c.txt:

{{#definedoc "foo" a.b}}
This is content foo
{{/definedoc}}

{{#definedoc "bar" a.c}}
This is content bar
{{/definedoc}}

{{#definedoc "baz" a.d}}
This is content baz
{{/definedoc}}

The following:

var str = fs.readFileSync('fixtures/c.txt', 'utf8');
console.log(blocks(str));

Results in:

{ definedoc:
   { foo:
      { args: [ 'a.b' ],
        content: 'This is content foo',
        orig:
         [ '{{#definedoc "foo" a.b}}',
           'This is content foo',
           '{{/definedoc}}' ] },
     bar:
      { args: [ 'a.c' ],
        content: 'This is content bar',
        orig:
         [ '{{#definedoc "bar" a.c}}',
           'This is content bar',
           '{{/definedoc}}' ] },
     baz:
      { args: [ 'a.d' ],
        content: 'This is content baz',
        orig:
         [ '{{#definedoc "baz" a.d}}',
           'This is content baz',
           '{{/definedoc}}' ] } } }

Context

Context can be passed as thisArg when three arguments are passed, and/or on options.locals.

Example

blocks(str, {locals: {a: {name: 'AAA'}}});

And this block:

{{#apidoc "aaa" a}}
This is content <%= name %>
{{/apidoc}}

Results in the following object:

{ apidoc:
   { aaa:
      { args: [ 'a' ],
        context: { name: 'AAA' },
        content: 'This is content <%= name %>',
        orig:
         [ '{{#apidoc "aaa" a}}',
           'This is content <%= name %>',
           '{{/apidoc}}' ] }

The content can now be used to pass to any template engine with context as locals.

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert
Released under the MIT license

This file was generated by verb on December 02, 2014.

Keywords

block

FAQs

Package last updated on 03 Dec 2014

Did you know?

Socket

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.

Install

Related posts