jotdown_wasm: display version

mostly useful for the hosted version
This commit is contained in:
Noah Hellman 2023-04-04 19:27:20 +02:00
parent 0af2c46c57
commit 7a10767fed
6 changed files with 184 additions and 13 deletions

144
Cargo.lock generated
View file

@ -65,6 +65,15 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cc"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
dependencies = [
"jobserver",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -159,6 +168,28 @@ version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "form_urlencoded"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8"
dependencies = [
"percent-encoding",
]
[[package]]
name = "git2"
version = "0.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3826a6e0e2215d7a41c2bfc7c9244123969273f3476b939a226aac0ab56e9e3c"
dependencies = [
"bitflags",
"libc",
"libgit2-sys",
"log",
"url",
]
[[package]] [[package]]
name = "half" name = "half"
version = "1.8.2" version = "1.8.2"
@ -186,6 +217,16 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678" checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678"
[[package]]
name = "idna"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6"
dependencies = [
"unicode-bidi",
"unicode-normalization",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.2" version = "1.9.2"
@ -211,6 +252,15 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]]
name = "jobserver"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "jotdown" name = "jotdown"
version = "0.1.0" version = "0.1.0"
@ -219,6 +269,7 @@ version = "0.1.0"
name = "jotdown_wasm" name = "jotdown_wasm"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"git2",
"jotdown", "jotdown",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
@ -245,6 +296,30 @@ version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "libgit2-sys"
version = "0.13.2+1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a42de9a51a5c12e00fc0e4ca6bc2ea43582fc6418488e8f615e905d886f258b"
dependencies = [
"cc",
"libc",
"libz-sys",
"pkg-config",
]
[[package]]
name = "libz-sys"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.17" version = "0.4.17"
@ -282,19 +357,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]] [[package]]
name = "proc-macro2" name = "percent-encoding"
version = "1.0.51" version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "pkg-config"
version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "proc-macro2"
version = "1.0.56"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.23" version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
@ -387,12 +474,59 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "tinyvec"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50"
dependencies = [
"tinyvec_macros",
]
[[package]]
name = "tinyvec_macros"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "unicode-bidi"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.6" version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc"
[[package]]
name = "unicode-normalization"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921"
dependencies = [
"tinyvec",
]
[[package]]
name = "url"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
]
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.3.2"

View file

@ -17,3 +17,6 @@ wasm-bindgen = { version = "0.2", default-features = false }
[dependencies.web-sys] [dependencies.web-sys]
version = "0.3" version = "0.3"
[build-dependencies]
git2 = { version = "0.14", default-features = false }

View file

@ -1,6 +1,6 @@
WASM=pkg/jotdown_wasm_bg.wasm WASM=pkg/jotdown_wasm_bg.wasm
SRC=$(shell find src ../../src -name '*.rs') SRC=$(shell find . ../../src -name '*.rs')
${WASM}: ${SRC} ${WASM}: ${SRC}
wasm-pack build --release --target web wasm-pack build --release --target web

View file

@ -0,0 +1,17 @@
fn get_version() -> String {
if let Ok(repo) = git2::Repository::discover(".") {
if let Ok(describe) = repo.describe(&git2::DescribeOptions::new()) {
if let Ok(format) = describe.format(None) {
return format;
}
}
}
std::env::var("CARGO_PKG_VERSION").unwrap()
}
fn main() {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let dest_path = std::path::Path::new(&out_dir).join("version");
std::fs::write(&dest_path, get_version()).unwrap();
}

View file

@ -1,12 +1,16 @@
<div id="jotdown" style="flex-grow:1;display:flex;flex-direction:column"> <div id="jotdown" style="flex-grow:1;display:flex;flex-direction:column">
<script type="module"> <script type="module">
import init, { import init, {
jotdown_version,
jotdown_render, jotdown_render,
jotdown_parse, jotdown_parse,
jotdown_parse_indent, jotdown_parse_indent,
} from './pkg/jotdown_wasm.js'; } from './pkg/jotdown_wasm.js';
await init(); await init();
let version = document.getElementById("jotdown-version");
version.innerText = "jotdown-" + jotdown_version();
let output = document.getElementById("jotdown-output"); let output = document.getElementById("jotdown-output");
let input = document.getElementById("jotdown-input"); let input = document.getElementById("jotdown-input");
let fmt = document.getElementById("jotdown-fmt"); let fmt = document.getElementById("jotdown-fmt");
@ -35,7 +39,13 @@
// auto focus on input on load // auto focus on input on load
setTimeout(() => { input.focus(); }, 0); setTimeout(() => { input.focus(); }, 0);
</script> </script>
<div> <div style="display:flex;gap:1rem">
<div style="width:50%">
<span style="opacity:50%">
version: <span id="jotdown-version">(not loaded)</span>
</span>
</div>
<div style="width:50%">
<select id="jotdown-fmt"> <select id="jotdown-fmt">
<option value="preview">preview</option> <option value="preview">preview</option>
<option value="html">html</option> <option value="html">html</option>
@ -43,6 +53,7 @@
<option value="events_indent">events (indented)</option> <option value="events_indent">events (indented)</option>
</select> </select>
</div> </div>
</div>
<div id="jotdown-panes" style="display:flex;height:100%;gap:1rem"> <div id="jotdown-panes" style="display:flex;height:100%;gap:1rem">
<pre id="jotdown-input" contenteditable="true" placeholder="Input djot here" style="width:50%;height:100%;overflow:scroll;resize:none;box-sizing:border-box;margin:0">*Hello world!*</pre> <pre id="jotdown-input" contenteditable="true" placeholder="Input djot here" style="width:50%;height:100%;overflow:scroll;resize:none;box-sizing:border-box;margin:0">*Hello world!*</pre>
<pre id="jotdown-output" readonly style="width:50%;height:100%;overflow:scroll;box-sizing:border-box;margin:0"></pre> <pre id="jotdown-output" readonly style="width:50%;height:100%;overflow:scroll;box-sizing:border-box;margin:0"></pre>

View file

@ -3,6 +3,12 @@ use wasm_bindgen::prelude::*;
use jotdown::Render; use jotdown::Render;
use std::fmt::Write; use std::fmt::Write;
#[must_use]
#[wasm_bindgen]
pub fn jotdown_version() -> String {
include_str!(concat!(env!("OUT_DIR"), "/version")).to_string()
}
#[must_use] #[must_use]
#[wasm_bindgen] #[wasm_bindgen]
pub fn jotdown_render(djot: &str) -> String { pub fn jotdown_render(djot: &str) -> String {