You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

pug-walk

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pug-walk

Walk and transform a jade AST


Version published
Weekly downloads
1.5M
increased by1.23%
Maintainers
1
Install size
6.32 kB
Created
Weekly downloads
 

Package description

What is pug-walk?

The pug-walk npm package is designed for walking through the abstract syntax tree (AST) of Pug templates. It allows developers to traverse and manipulate nodes in the AST, enabling operations such as modification, filtering, and analysis of Pug templates.

What are pug-walk's main functionalities?

Walking the AST

This example demonstrates how to traverse a Pug template's AST to find all `div` tags and add an `id` attribute to them. The `walk` function takes the AST and a callback function that is applied to every node. If the node matches the criteria (a `div` tag in this case), a new attribute is added.

const pug = require('pug');
const pugWalk = require('pug-walk');

const ast = pug.parser.parse('div(class=classes)');
pugWalk.walk(ast, (node, replace) => {
  if (node.type === 'Tag' && node.name === 'div') {
    node.attrs.push({
      name: 'id',
      val: '"dynamic-id"',
      mustEscape: false
    });
    replace(node);
  }
});

Filtering Nodes

This code snippet shows how to filter nodes of a specific type, in this case, `Conditional` nodes, from a Pug template's AST. The `walk` function is used to traverse the AST, and nodes that match the specified condition are added to an array for further processing.

const pug = require('pug');
const pugWalk = require('pug-walk');

const ast = pug.parser.parse('div
  if condition
    p True
  else
    p False');
const filteredNodes = [];
pugWalk.walk(ast, node => {
  if (node.type === 'Conditional') {
    filteredNodes.push(node);
  }
}, {includeDependencies: true});

Other packages similar to pug-walk

Readme

Source

jade-walk

Walk and transform a jade AST

Build Status Dependency Status NPM version

Installation

npm install jade-walk

Usage

var lex = require('jade-lexer');
var parse = require('jade-parser');
var walk = require('jade-walk');

var ast = walk(parse(lex('.my-class foo')), function before(node, replace) {
  // called before walking the children of `node`
  // to replace the node, call `replace(newNode)`
  // return `false` to skip descending
  if (node.type === 'Text') {
    replace({ type: 'Text', val: 'bar', line: node.line });
  }
}, function after(node, replace) {
  // called after walking the children of `node`
  // to replace the node, call `replace(newNode)`
}, {includeDependencies: true});
assert.deepEqual(parse(lex('.my-class bar')), ast);

License

MIT

Keywords

FAQs

Package last updated on 12 Dec 2015

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc