$ npm install muggle-pug
I wrote muggle-pug for vuejs/language-tools, so that a Vue template written in pug reads as HTML to the language server, and every result maps back to the pug code. It compiles pug to HTML as the pieces of muggle-string: ranges of the pug code, kept wherever the HTML is the pug code.
It depends only on pug-lexer, pug-parser and the Piece type of muggle-string, and builds no mappings: every consumer turns the pieces into the mappings of its own tooling.
npm install muggle-pug. It ships as ES modules only. CommonJS code can require it on Node 20.19 and later, or 22.12 and later.
import { compile } from 'muggle-pug';
const pugCode = 'div.foo hello';
const { pieces, ast, error } = compile(pugCode);
// pieces: [..., { start: 8, end: 13 }, ...]
const html = pieces.map(piece => typeof piece === 'string' ? piece : piece.text ?? pugCode.slice(piece.start, piece.end)).join('');
// html: '<div class="foo">hello</div>'
pieces: the HTML, as muggle-string pieces of the pug code, with no source and no data. A range without text keeps the pug code at start..end. A range with text is HTML that stands for its range: " for the . of .foo, or, as an empty range, a position, such as < at the start of a tag. A string, an end tag, stands for no range.ast: the AST of pug-parser, with 1-based lines and columns of the pug code.error: the first error of pug, with its code, message, offset, and 0-based line and column; undefined if there is none.The HTML is for tools, not what pug renders: an HTML parser reads it as the elements, attributes, text and comments written in the pug code, and each of them is the pug code where it can be. Nothing is evaluated, as a value that is not its code maps to nothing in it.
class="a" + b, `${a}` or 'a\'b' is code that only pug's runtime gives a value; here it stays code.<, > and /> at positions. An element without content closes with />, so that no void element has an end tag. An end tag stands for no range, followed by the end of the element as an empty range: as the range of the name, or as a position, a tool that edits or shows both names of an element, such as a rename or a highlight, would have the one range twice. An implicit div is at the position of the tag..foo or #foo, is class=" or id=" whose name and quote stand for its . or #, and " at its end. Each is its own attribute, as .foo(class="bar") makes two class attributes anyway (the Vue plugin of vuejs/language-tools ignores the duplicate).! of !=, so that the name is the name.", or else '. With both in it, its " are ', so that the value ends where the literal does. The \ of a line continuation in it is a space, so that the value as an expression of Vue has no invalid char.//- too, is <!-- for its // and --> at its end, for the directive comments of Vue, such as @vue-ignore.pug stops at its first error, but a template that is being written still needs HTML. So muggle-pug leaves out the line that pug fails on, one at a time, until pug reads the rest: pieces and ast cover the other lines, and error is the first error.
const { pieces, error } = compile('div\n p(cl\n span hi');
// html: '<div><span>hi</span></div>'
// error: { code: 'PUG:NO_END_BRACKET', line: 2, column: 9, ... }
compile does not throw: an error that pug throws without a position, such as the TypeError of div# at the end of the code, is at where pug stops, with the name or code of the error.
Vue dedents a pug template, but pug reads an indented first line as text, so pug is given the template without the indentation that all its lines share; the pieces, the AST and the error are all of the written template.
The pieces carry no source, as all of them are of the one pug code. To render them with renderWithSourceMap of muggle-string, give each range its source:
import { compile } from 'muggle-pug';
import { renderWithSourceMap } from 'muggle-string';
const source = { name: 'App.pug', content: 'div.foo hello' };
const { pieces } = compile(source.content);
const { text, map } = renderWithSourceMap(
sink => pieces.forEach(piece => sink(typeof piece === 'string' ? piece : { ...piece, source })),
(from: typeof source) => from.name,
);
console.log(text);
// <div class="foo">hello</div>
console.log(map.mappings);
// AAAA,CAAA,GAAG,CAAA,KAAA,CAAA,CAAC,GAAG,CAAA,CAAC,K;
console.log(map.names);
// [], as no HTML renames a name of the pug code
© 2010 - cnpmjs.org x YWFE | Home | YWFE