The Official TapirMD Libraries
TapirMD official maintains libraries for Zig, WebAssembly (WASM), JavaScript, and Go.
The Zig library is fully featured, while the others currently support only HTML generation (from TapirMD documents) and TapirMD document formatting.
The Zig library
The repository containing the Zig library is hosted at https://github.com/tapirmd/tmd.
To use the library, the TapirMD dependency entry should be put in the
build.zig.zon file of your Zig project. It is recommended to choose a tagged version as the dependency. The following example uses the v0.0.5 version tag.
build.zig.zon
...
.tmd = .{
.url = "https://github.com/tapirmd/tmd/archive/refs/tags/v0.0.5.zip",
},
...
The library provides a
tmd module which can be imported into the modules of your Zig project.
build.zig
...
const tmdModule = b.dependency("tmd", .{
.target = target,
.optimize = optimize,
}).module("tmd");
...
myModule.addImport("tmd", tmdModule);
...
Here is an example Zig project which uses the TapirMD Zig library.
The WASM library
The steps to build the TapirMD WASM library:
-
Install Zig, if haven't. The latest version tip (master) version is recommended.
-
Get the source code of the TapirMD project. You can clone it (install Git if needed) or download its source code.
-
Open your terminal or command prompt, and change directory to the TapirMD project folder, then run
zig build -Doptimize=ReleaseSmall wasm
If the build is successful, the outputtmd.wasmbinary file will be located in thelibfolder under the project localzig-outfolder.
The output
tmd.wasm binary file can be used in various languages. The official TapirMD JavaScript and Go libraries are both built upon WASM.
The WASM binary file exports several APIs:
-
lib_versionreturns the memory offset of the version string of the library. The version string is C-style string which ends with 0. -
buffer_offsetreturns the memory offset of the buffer for communicating between WASM and host apps. The input TapirMD document content with a length header should be placed at the beginning of the buffer. -
tmd_parseparses the input TapirMD document and returns the memory offset of unused buffer (for communicating between WASM and host apps). The following APIs should put their options data at the returned memory offset. -
tmd_titlegenerates the title (without any formatting elements) of the input TapirMD document and returns the memory offset of the title with a length header. If the title length is0xFFFFFFFF, it means the input TapirMD document doesn't define a title. -
tmd_to_htmlgenerates HTML content from the input TapirMD document content and returns the memory offset of the output HTML content with a length header. -
tmd_formatformats the input TapirMD document and returns the memory offset of the output formatted result with a length header. If the result length is0xFFFFFFFF, it means the formatted result is identical to the input TapirMD document.
Every memory offset return result is a
int32 value. A negative offset means an error and the memory offset of the error string (a C-style string) is the absolute value of the negative offset minus one.
Host apps should provide an
env module which contains a print function. The print function takes 4 uint32 and one int32 parameters.
-
the 1st
uint32parameter means the memory offset of the first string. -
the 2nd
uint32parameter means the length (in bytes) of the first string. -
the 3rd
uint32parameter means the memory offset of the second string. -
the 4th
uint32parameter means the length (in bytes) of the second string. -
the final
int32parameter is just a plain integer value.
For implementation details, please reference the official TapirMD JavaScript and Go libraries (see below).
The JavaScript library
Currently, the official TapirMD JavaScript library can be only used in browser environments.
The steps to build the TapirMD JavaScript library:
-
Install Zig, if haven't. The latest version tip (master) version is recommended.
-
Get the source code of the TapirMD project. You can clone it (install Git if needed) or download its source code.
-
Open your terminal or command prompt, and change directory to the TapirMD project folder, then run
zig build -Doptimize=ReleaseSmall js
If the build is successful, the outputtmd-with-wasm.jsfile will be located in thelibfolder under the project localzig-outfolder.
Please read the source code of the online play page to get how to use the JavaScript library.
The Go library
The TapirMD official Go library is hosted at https://github.com/go101/tmd.go.
The library repository includes an example demonstrating its usage.
The API documentation is available at https://pkg.go.dev/go101.org/tmd.go.