Skip to content

Latest commit

 

History

History

js-api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

LOL HTML JavaScript API

Example

'use strict';

const { HTMLRewriter } = require('lol-html'); // path/to/lol-html.js

const chunks = [];
const rewriter = new HTMLRewriter('utf8', (chunk) => {
  chunks.push(chunk);
});

rewriter.on('a[href]', {
  element(el) {
    const href = el
      .getAttribute('href')
      .replace('http:', 'https:');
    el.setAttribute('href', href);
  },
});

[
  '<div><a href=',
  'http://example.com>',
  '</a></div>',
].forEach((part) => {
  rewriter.write(Buffer.from(part));
});

rewriter.end();

const output = Buffer.concat(chunks).toString('utf8');
console.log(output);

Building

rustup update # https://rustup.rs
cargo install wasm-pack

wasm-pack build --target nodejs --release