You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

pug-walk

Package Overview
Dependencies
Maintainers
3
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 pug AST

2.0.0
latest
Source
npmnpm
Version published
Weekly downloads
1.6M
-17.63%
Maintainers
3
Weekly downloads
 
Created

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

Keywords

pug

FAQs

Package last updated on 25 May 2020

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