trys / sergey

A tiny lil' static site generator
https://sergey.trysmudford.com
MIT License
302 stars 39 forks source link

Use CSS selector query-based instead of Regular Expressions #43

Open guesant opened 4 years ago

guesant commented 4 years ago

Query and change HTML tags with CSS Selectors using the modules CSSselect, htmlparser2 and domutils. The sergey's source code uses a lot of while loops and Regular Expressions. This proposal can handle html tags by querying with CSSselect and making a DOM-based changes with domutils.

Before was:

while ((m = patterns.templates.exec(content)) !== null) {
  if (m.index === patterns.templates.lastIndex) {
    patterns.templates.lastIndex++;
  }
  const [find, name, data] = m;
  if (name !== "default") {
    slots.default = slots.default.replace(find, "");
  }
  slots[name] = formatContent(data);
}

Now can be:

const { nodes } = queryNodesByHTML({
  html: content,
  selector: patterns.template,
});
nodes.forEach((node) => {
  const find = domutils.getOuterHTML(node);
  const name = domutils.getAttributeValue(node, "name");
  const data = domutils.getInnerHTML(node);

  if (name !== "default") {
    slots.default = slots.default.replace(find, "");
  }
  slots[name] = formatContent(data);
});

Some implemented functions:


Maybe related with #36 #24

vercel[bot] commented 4 years ago

Deployment has failed due to an internal error. (code: jDTv0ZV)

Contact our support with support@vercel.com for more information.