npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

posthtml-sugarml

v1.0.0-alpha3

Published

Sugar Syntax for PostHTML

Downloads

27

Readme

npm deps tests coverage code style chat

npm i -S posthtml-sugarml
import { readFileSync } from 'fs'

import posthtml from 'posthtml'
import sugarml from 'posthtml-sugarml'

const html = readFileSync('./index.sml', 'utf8')

posthtml()
  .process(html, { parser: sugarml() })
  .then((result) => console.log(result.html))

This parser is very loose with its rules and standards. It is not responsible for enforcing good style or conventions, it's simply responsible for compiling your code. This means that you can use all sorts of invalid characters in attribute and tag names, and indentation rules are extremely loose.

Indentation

This parser determines how tags are nested based on indentation. For example:

.first-level
  .second-level
    .third-level Hi!
  .second-level

This would be compiled into the following html output:

<div class="first-level">
  <div class="second-level">
    <div class="third-level">Hi!</div>
  </div>
  <div class="second-level"></div>
</div>

As long as one line is indented with more characters than the last, it will be nested. It doesn't matter if the number of characters that you use is consistent, or if they are spaces or tabs, or anything else. It's just the number of space characters used to indent, that's it. So you can get away with very messy code, if you want, but that's what linters are for.

Doctypes

The doctype is a special tag, and is handled specially. If the first word on the first line of your template is doctype, it will be parsed as a doctype. Anything after this word will be added to the tag. So for example:

doctype html >>> <!DOCTYPE html>
doctype HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Tags

A tag is written simply as the name of the tag. Tag names must start with a letter, then after that can contain any character other than #, ., (, :, or a space/tab. These character limitation are in place solely because of the language's syntax requirements.

So, for example, these tag names are valid and will compile correctly (although I would not advise using a tag with characters other than letters and hyphens personally):

tag
tag!
tag-name
tag_name
tag@name

However, these tag names will not compile into the results you expect

tag.name
tag:name
tag(name
tag name

Fortunately, it is not advisable to have custom html tags that look anything like these anyway, so you should be in the clear as long as you are writing reasonable html.

Nested Tags

Sometimes you don't really want to indent nested tags when they are short enough to be placed on one line. When this happens, you can use a colon instead of a newline and indent to nest. For example:

ul
  li: a(href='#') link 1
  li: a(href='#') link 2

You can nest as many wrapper tags on a single line as you want, as long as they are all separated by a colon immediately following the tag. However If the tag has content, it cannot be nested with a colon. For example:

.wrap: .wrap2: .wrap3 hello! // this works fine
.wrap hello!: .wrap2         // this doesn't work

Shorthands

There is a shorthand for adding classes and IDs to your tags, which is exactly the same as it is in just about every other whitespace-significant html parser, and the same as CSS. For example:

p#main => <p id="main"></p>
p.main => <p class="main"></p>
p#uid.app.active => <p id="uid" class="app active"></p>

You can chain as many classes and IDs in this manner as you want. If you do not use an element name, it will assume you want a div. For example:

#main => <div id="main"></div>

Attributes

Attributes fall between parentheses directly after a tag's name. They are space-separated and can be either boolean (key, no value), or key/value pairs. For example:

input(checked) => <input checked>
input(type='text') => <input type="text">
input(type='checkbox' checked) => <input type="checkbox" checked>

You can quote your attribute values or not, your choice (although we would recommend quoting). However, if the value contains a space, it must be quoted. For example:

div(class=foo) => <div class="foo"></div>
div(class=foo bar) => <div class="foo" bar></div>
div(class='foo bar') => <div class="foo bar"></div>

Attributes can contain any character other than = or a space. If you value is quoted, it can contain any value other than a quote (that will end the attribute), and if it's not quoted, it can contain any value other than a quote or space. So even attributes with special characters (found sometimes in certain front-end frameworks like vue and angular) work fine. For example:

div(:bind='focus') >>> <div :bind="click"></div>
div(*ngFor='foo in bar') >>> <div *ngFor="foo in bar"></div>
div(@click='handleClick') >>> <div @click="handleClick"></div>

Content

If you need to mix up text content alongside inline tags and such, you can use the pipe character for this as such:

p
  | Here's some text
  strong And some bold text
  | ...and some more text

This would render as:

<p>Here's some text <strong>and some bold text</strong> ...and some more text</p>

For any type of content transforms that are more complex than this, we recommend checking out posthtml-content.

doctype html
html
  head
    title Testing
  body#index
    h1 Hello world!
    p.intro Wow what a great little language! Some features:
    ul(data-list='yep' @sortable)
      li: a(href='#') whitespace significant!
      li: a(href='#') simple classes and ids!
    footer
      | Thanks for visiting
      span see you next time!
import { readFileSync } from 'fs'

import posthtml from 'posthtml'
import sugarml from 'posthtml-sugarml'

const html = readFileSync('./index.sml', 'utf8')

posthtml()
  .process(html, { parser: sugarml() })
  .then((result) => console.log(result.html))
<!DOCTYPE html>
<html>
  <head>
    <title>Testing</title>
  </head>
  <body id="index">
    <h1>Hello world!</h1>
    <p class="intro">Wow what a great little language! Some features:</p>
    <ul data-list="yep" @sortable>
      <li><a href="#">whitespace significant!</a></li>
      <li><a href="#">simple classes and ids!</a></li>
    </ul>
    <footer>
      Thanks for visiting
      <span>see you next time!</span>
    </footer>
  </body>
</html>