Actmd: Extension for runtime evaluated elements

I would like to present an idea of mine that I have been working on for some time. I would like to build a Commonmark parser with active elements, similar to Razor for HTML. The goal is to parse the document into an AST and generate source code from it to produce the HTML output at runtime.

In this way, functionality could easily be extended without changing the spec. Everybody could write its own functions to be executed at runtime.

My first usage of the @ is the command @next_attr("name", "value") to attach attributes to the following HTML element and the commands @m(…) and @am(…) for MathJAX and AsciiMath code.

@next_attr("id", "sec-1")
# A topic with math

@m(
c &= \sqrt{a^2 + b^2}\\
  &= ...
)

or the same as inline with AsciiMath @am(7*4).

But the @ syntax does not only allow adding new styling elements but also to add whole code blocks which allows fetching data from a database or doing any calculation.

@if let Some(user) = get_user() {
# Hi @user
} else {
# Hi guest

Please, login
}

Having code that generates the output makes it possible to stack documents, i.e. a base documents gets a reference to the content function which it calls at runtime.

<body>
<main>
@content()
</main>
</body>

I’ve also added a head at the front of the document to set some metadata like title, tags, description and creation-date in an RFC 822 like manner. And I’ve added comments @// and @/* … */ to exclude sections from the document.

The current state of my parser actmd is heavy WIP. It’s not complete and doesn’t pass all tests from the Commonmark spec. I’ve also made some diversions from the spec to simplify the code:

  • I’ve dropped indented code blocks,
  • hard line-breaks with spaces,
  • no # at the end of ATX headings
  • no setext headings
  • I only treat SPACE, TAB, CR and NL as whitespace

There are much more details to tell, but for the first, it should give a broad impression of the idea.