Functions to help with making html-like nodes in a unified-latex
Abstract Syntax Tree (AST).
For example, <p>foo</p>
can be stored as \html-tag:p{foo}
in unified-latex
. Because -
and :
are special characters, they cannot appear in a macro name, so there is no risk of name conflicts.
These macros are created programmatically, so special characters can be inserted.
If you are converting LaTeX to HTML, these functions may be used as an intermediate format.
unified-latex-to-hast
first processes a document into html-like nodes and then
converts the resulting document to HAST/HTML. Any html-like macros that are created
will be transparently converted to HAST/HTML. For example, if you wanted to convert
all \foo
macros into <br />
tags, you could first preprocess your unified-latex AST
and replace all occurrences of \foo
with htmlLike({ tag: "br" })
. When the AST is converted
to HTML, the html-like macros will be rendered as <br />
tags.
npm install @unified-latex/unified-latex-util-html-like
This package contains both esm and commonjs exports. To explicitly access the esm export,
import the .js
file. To explicitly access the commonjs export, import the .cjs
file.
Extract the contents/attributes/tag from an html-like macro.
function extractFromHtmlLike(macro: Ast.Macro): {
tag: string;
attributes: Record<string, string | number | boolean | object>;
content: Ast.Node[];
};
Parameters
Param | Type |
---|---|
macro | Ast.Macro |
Make an html-like node storing content
. The node is a macro and content
as well
as any attributes can be extracted or further processed. Collisions are avoided with existing
macros because all macros are prefixed with html-tag:
or html-attribute:
, which contain
special characters that normal macros cannot have.
function htmlLike({
tag,
content,
attributes,
}: {
tag: string;
content?: Ast.Node | Ast.Node[];
attributes?: object;
}): Ast.Macro;
Parameters
Param | Type |
---|---|
{ tag, content, attributes, } | Omitted |
Determine whether the node is an html-like macro.
function isHtmlLike(node: any): boolean;
Parameters
Param | Type |
---|---|
node | any |
Determine whether the node is an html-like macro for an attribute.
function isHtmlLikeAttribute(node: any): boolean;
Parameters
Param | Type |
---|---|
node | any |
Determine whether the node is an html-like macro for a tag.
function isHtmlLikeTag(node: any): boolean;
Parameters
Param | Type |
---|---|
node | any |