pug-walk-extract-text
Version:
Walk and transform a Pug AST, extracting text nodes from e.g. script and style tags
55 lines (38 loc) • 2.35 kB
Markdown
A small module for extracting text (e.g. inline `script` and `style` tags) from [Pug] files (formerly called Jade)!
Used by [eslint-plugin-pug].
[Pug]: https://pugjs.org
[eslint-plugin-pug]: https://github.com/myfreeweb/eslint-plugin-pug
## Installation
Install with [npm], obviously:
```bash
npm install --save-dev pug-parser pug-lexer pug-walk-extract-text
```
[npm]: https://www.npmjs.com
## Usage
```javascript
var lexer = require('pug-lexer')
var parser = require('pug-parser')
var walkExtract = require('pug-walk-extract-text')
function shouldExtract (node) {
return node.type === 'Tag' && (node.name === 'script' || node.name === 'style')
}
var filename = 'some-file.pug'
var text = fs.readFileSync(filename, { encoding: 'utf-8' })
var results = walkExtract(parser(lexer(text, filename), filename), text, shouldExtract)
// [
// { text: "body { background: #efefef; color: #444; }\n html { font-size: 105%; }",
// indent: 6, line: 9,
// node: { attrs: [], ..., name: 'style', type: 'Tag' } },
// { text: "console.log({\n scri: 'pt'\n})",
// indent: 6, line: 15,
// node: { attrs: [], ..., name: 'script', type: 'Tag' } },
// ]
```
Please feel free to submit pull requests!
Bugfixes and simple non-breaking improvements will be accepted without any questions :-)
By participating in this project you agree to follow the [Contributor Code of Conduct](http://contributor-covenant.org/version/1/4/).
This is free and unencumbered software released into the public domain.
For more information, please refer to the `UNLICENSE` file or [unlicense.org](http://unlicense.org).