@beforesemicolon/markup

中文文档
1.21.0发布于 2026/11/01BSD-3-ClauseTypeScriptESM + CJS零依赖近 30 天下载 3567

Reactive HTML Templating System

原始使用文档
文档目录9 节
  1. Motivation
  2. How does it work?
  3. Install
  4. Use directly in the Browser
  5. Usage
  6. Serialization and event safety
  7. AI and agent context
  8. Ecosystem
  9. Community

Markup

Reactive HTML Templating System

Static Badge MD Docs npm npm Test

Markup is a JavaScript reactive templating system built to simplify how you build Web user interfaces using web standards with minimal enhancements to the native web APIs as possible.

It consists of 3 main APIs with additional utilities to simplify things even further:

  • html: A JavaScript tagged function that allows you to represent the DOM using a template literal string;
  • state: A simple state tracking API that allows you to define reactive data as you wish;
  • effect: A straight forward way to define things that need to happen when certain states change;

Motivation

JavaScript, Web Standards and APIs give you everything you need to build any web project you want. What is often painful to deal with is the DOM and reactivity, especially as the project gets bigger or require multiple people collaboration.

Markup was created to give you all the reactivity you need while making it simple to define your HTML in JavaScript without introducing a new syntax, or requiring a build, or a big library you need to ship to the client.

From this:

let count = 0

// tedious DOM definition and manipulation
const p = document.createElement('p')
p.textContent = `count: ${count}`

const btn = document.createElement('button')
btn.type = 'button'
btn.textContent = 'count up'

// limiting event driven
btn.addEventListener('onclick', () => {
    count += 1
    p.textContent = `count: ${count}`

    if (count > 10) {
        alert('You counted passed 10!')
    }
})

document.body.append(p, btn)

To this:

// reactive data
const [count, updateCount] = state(0)

// data driven
effect(() => {
    if (count() > 10) {
        alert('You counted passed 10!')
    }
})

const countUp = () => {
    updateCount((prev) => prev + 1)
}

// reactive DOM/templates
html`
    <p>count: ${count}</p>
    <button type="button" onclick="${countUp}">count up</button>
`.render(document.body)

How does it work?

Markup uses template literals and Functions to do everything.

  • Functions: JavaScript functions are perfect for lazy evaluations which makes it perfect for reactivity. Markup uses functions everywhere, from internals, defining state, effects, utilities, etc.
  • Template Literals: The template literal is used to represent HTML in JavaScript and to avoid to reinvent the wheel. Everything else is enforced by web standards.

Literally everything else is how you know it to be in plain HTML, CSS and JavaScript.

We believe in a simple way to build the web without the jargon of a framework, complex project setups, new syntax, all to spit out HTML, CSS and JavaScript at the end.

Examples

This is a simple example of a button, but you can check:

Install

npm install @beforesemicolon/markup

or

yarn add @beforesemicolon/markup

Use directly in the Browser

This library requires no build step. The CDN browser build is about 13.9 KB gzip (40.2 KB uncompressed, measured from dist/client.js).

<!-- Grab the latest version -->
<script src="https://unpkg.com/@beforesemicolon/markup/dist/client.js"></script>

<!-- Or a specific version -->
<script src="https://unpkg.com/@beforesemicolon/[email protected]/dist/client.js"></script>

Usage

<div id="app"></div>

<script>
    const { html, state, effect } = BFS.MARKUP

    html`<h1>Hello World</h1>`.render(document.getElementById('app'))
</script>

BFS.MARKUP exposes the same runtime APIs as the ESM entry point, including helpers such as visible and trusted-markup utilities such as unsafeHTML.

Dynamic values belong in html interpolations. Markup applies those values without parsing them as HTML:

const message = '<strong>hello</strong>'
html`<p>${message}</p>` // renders the markup characters as text

When trusted or independently sanitized markup must be parsed deliberately, use the explicit unsafeHTML API. Never pass user-controlled content to it:

import { unsafeHTML } from '@beforesemicolon/markup'

const trustedIcon = unsafeHTML('<svg aria-hidden="true">...</svg>')

Serialization and event safety

toString() escapes text at the template root as well as inside elements and preserves HTML comments. It rejects content that could escape a script, style, or other text container when the result is parsed again. For JSON data blocks, escape every < after calling JSON.stringify:

const json = JSON.stringify(data).replace(/</g, '\\u003c')
const output = html`<script type="application/json">
    ${json}
</script>`.toString()

Script and style interpolations still require trusted code; Markup does not sanitize JavaScript or CSS. Boundary checks ignore tag-name casing and apply across namespaces, including SVG/MathML nodes that may reparse as HTML. These conservative checks also apply inside foreign-namespace wrappers. See the SSR documentation for serialization restrictions and migration details.

Dynamic on* event bindings accept one function or [function, options] interpolation, optionally surrounded by whitespace. String handlers and mixed literal/interpolated event strings are rejected. Use onclick="${handleClick}" instead of constructing inline JavaScript. Explicitly observed, non-native custom-element properties such as one remain ordinary property bindings. Templates can be constructed before the custom element is registered; define the element before mounting so those properties can be recognized. Deferred mixed event bindings are rejected before any binding is evaluated or written. When a mounted binding receives a new handler, Markup updates a stable delegating listener instead of removing and adding a DOM listener. once is rearmed for the new binding, while changes to capture, passive, or signal still replace the native registration so browser semantics remain intact.

AI and agent context

  • llms.txt is the concise, package-owned API contract and coding guide for AI tools.
  • llms-full.txt contains the complete resolved documentation and examples.

The source for the concise contract lives in docs/llms.txt, so its behavior and recommendations are reviewed and versioned with Markup.

Ecosystem

  • Web Component adds reactive native Custom Elements and re-exports Markup.
  • Router adds URL navigation, route elements, guards, and route data on top of Web Component.
  • Intl adds locale scopes, messages, and internationalized formatting on top of Web Component.

Router and Intl are complementary and can be used together. Markup remains the shared rendering and reactivity foundation across all packages.

Community

版本与发布记录

@beforesemicolon/markup 共发布 120 个版本,首次发布于 2023/11/05;过去 12 个月发布了 16 个版本。

版本发布日期距今
1.21.0latest2026/11/016 天前
1.21.0-nextnext2026/10/2513 天前
1.20.02026/08/312 个月前
1.20.2-next2026/08/252 个月前
1.20.1-next2026/08/222 个月前
1.20.0-next2026/08/222 个月前

关于 @beforesemicolon/markup 的常见问题

@beforesemicolon/markup 是什么?

Reactive HTML Templating System

如何安装 @beforesemicolon/markup?

在项目目录运行 npm install @beforesemicolon/markup,或使用 pnpm add / yarn add / bun add。

@beforesemicolon/markup 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beforesemicolon/markup 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@beforesemicolon/markup 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.13.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@beforesemicolon/markup 可以商用吗?

@beforesemicolon/markup 使用 BSD-3-Clause 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。另外不得用原作者名义为衍生产品背书。以上为概括,不构成法律意见,请以许可原文为准。

@beforesemicolon/markup 的下载量有多少?

根据 npm 官方下载统计,@beforesemicolon/markup 在 2026/10/08 – 2026/11/06 这 30 天内被下载 3,567 次,日均约 119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beforesemicolon/markup 还在维护吗?

最新版本 1.21.0 发布于 2026/11/01(6 天前);至今共发布 120 个版本,其中过去 12 个月发布了 16 个;首次发布于 2023/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。