Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Latest commit

 

History

History
81 lines (58 loc) · 2.22 KB

README.md

File metadata and controls

81 lines (58 loc) · 2.22 KB

vimwiki wasm

Provides wasm bindings for vimwiki library.

Usage

npm install vimwiki-wasm

Examples

<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
    import init, { parse_vimwiki_str } from './vimwiki_wasm.js';

    async function run() {
        // If building as web, need to do this
        await init();

        // Load the code snippet from a dom element
        const code = document.getElementById("vimwiki-snippet");

        // Parse the code into an object
        const page = parse_vimwiki_str(code.innerText);

        // Find regions in code that contain headers and highlight them by
        // transforming that text into spans with colors
        const regions = page.descendants
          .filter(e => e.is_block())
          .map(e => e.into_block())
          .filter(e => e.is_header())
          .map(e => e.into_header().region);

        // For each header's region in the loaded text...
        Object.values(regions).forEach(region => {
            // Select the header
            const range = new Range();
            range.setStart(code.firstChild, region.offset);
            range.setEnd(code.firstChild, region.len);

            // Build a colored version
            const colored = document.createElement("span");
            colored.style.color = "red";
            colored.innerText = range.toString();

            // Swap the contents with the colored version
            range.deleteContents();
            range.insertNode(colored);
        });

        // Render vimwiki as HTML and inject into output destination
        const output = document.getElementById("vimwiki-output");
        output.insertAdjacentHTML("afterbegin", page.to_html_str());
    }

    run();
</script>

Building from source

Compiling for webpack bundler:

wasm-pack build --target bundler

Compiling for nodejs:

wasm-pack build --target nodejs

License

This project is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or apache-license) MIT license (LICENSE-MIT or mit-license) at your option.