miki

Miki: personal wiki with the fmd markup format
git clone https://wtenison.com/repo/miki.git
Log | Files | Refs | README | LICENSE

commit 6e945c35e16dd20048abb51137ca30aff2f36ff1
parent 1975cb4f9188c482aa1ea5f362181bfb81e641df
Author: weetat <wtenison@protonmail.com>
Date:   Tue, 15 Apr 2025 23:01:56 +0000

Fix subject lookup when a query string is present

handleView built the template path from r.URL.String(), which includes
?s=..., so /subject?s=foo never found its directory. Use r.URL.Path
instead. The subject script now waits for the fmd load and only sets
the title if it succeeded.

Diffstat:
Msrc/network/routes.go | 6+++---
Mweb/global/wasm_exec.js | 75+++++++++++++++++++++++++++++++++++++++++++--------------------------------
Mweb/subject/script.js | 19+++++++++----------
3 files changed, 55 insertions(+), 45 deletions(-)

diff --git a/src/network/routes.go b/src/network/routes.go @@ -40,7 +40,7 @@ func makeServerRoutes() { func handleView(w http.ResponseWriter, r *http.Request) { - _rPkg, _err := mfs.MakeDirectoryPkg(filepath.Join(mfs.Dirs["web"], r.URL.String()), mfs.SEARCH_DEPTH_ZERO, "*.html") + _rPkg, _err := mfs.MakeDirectoryPkg(filepath.Join(mfs.Dirs["web"], r.URL.Path), mfs.SEARCH_DEPTH_ZERO, "*.html") if _err != nil { log.Printf("_ERROR: network: handleView(\"/view\"): %s", _err) http.NotFound(w, r) @@ -53,7 +53,7 @@ func handleView(w http.ResponseWriter, r *http.Request) { // Create template from package. _tmpl, _err := template.ParseFiles(_rPkg.DependencyFiles...) if _err != nil { - _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ParseFiles: %s", r.URL.String(), _err) + _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ParseFiles: %s", r.URL.Path, _err) log.Println(_errStr) http.Error(w, _errStr, http.StatusInternalServerError) return @@ -61,7 +61,7 @@ func handleView(w http.ResponseWriter, r *http.Request) { // Render template. if _err = _tmpl.ExecuteTemplate(w, "index.html", nil); _err != nil { - _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ExecuteTemplate: %s", r.URL.String(), _err) + _errStr := fmt.Sprintf("_ERROR: network: handleView(\"%s\"): template.ExecuteTemplate: %s", r.URL.Path, _err) log.Println(_errStr) http.Error(w, _errStr, http.StatusInternalServerError) return diff --git a/web/global/wasm_exec.js b/web/global/wasm_exec.js @@ -240,16 +240,16 @@ this.importObject = { wasi_snapshot_preview1: { // https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_write - fd_write: function(fd, iovs_ptr, iovs_len, nwritten_ptr) { + fd_write: function (fd, iovs_ptr, iovs_len, nwritten_ptr) { let nwritten = 0; if (fd == 1) { - for (let iovs_i=0; iovs_i<iovs_len;iovs_i++) { - let iov_ptr = iovs_ptr+iovs_i*8; // assuming wasm32 + for (let iovs_i = 0; iovs_i < iovs_len; iovs_i++) { + let iov_ptr = iovs_ptr + iovs_i * 8; // assuming wasm32 let ptr = mem().getUint32(iov_ptr + 0, true); let len = mem().getUint32(iov_ptr + 4, true); nwritten += len; - for (let i=0; i<len; i++) { - let c = mem().getUint8(ptr+i); + for (let i = 0; i < len; i++) { + let c = mem().getUint8(ptr + i); if (c == 13) { // CR // ignore } else if (c == 10) { // LF @@ -384,7 +384,7 @@ mem().setUint8(ret_addr + 8, 1); } catch (err) { storeValue(ret_addr, err); - mem().setUint8(ret_addr+ 8, 0); + mem().setUint8(ret_addr + 8, 0); } }, @@ -409,7 +409,7 @@ // func valueInstanceOf(v ref, t ref) bool "syscall/js.valueInstanceOf": (v_ref, t_ref) => { - return unboxValue(v_ref) instanceof unboxValue(t_ref); + return unboxValue(v_ref) instanceof unboxValue(t_ref); }, // func copyBytesToGo(dst []byte, src ref) (int, bool) @@ -541,32 +541,43 @@ })(); function loadFMDFileRaw(url, container) { - return fetch(url) - .then(response => response.text()) - .then(text => { - // Load the text into the text editor & buffer - _textBUffer = text - }) - .then(() => { - // If FMD is valid, render to HTML - container.innerHTML = _textBUffer - }) + return fetch(url) + .then(response => response.text()) + .then(text => { + // Load the text into the text editor & buffer + _textBUffer = text + }) + .then(() => { + // If FMD is valid, render to HTML + container.innerHTML = _textBUffer + }) } function loadFMDFileHTML(url, container) { - return fetch(url) - .then(response => response.text()) - .then(text => { - // Load the text into the text editor & buffer - _textBUffer = text - }). - then(() => { - // If FMD is valid, render to HTML - if (isFMDValid(0, _textBUffer)) { - container.innerHTML = interpretFMD(0, _textBUffer) - } else { - container.innerHTML = "FMD Parsing Failed" - } - }) - .catch(err => console.error('> ERROR:fetch:failed to load FMD url:', err)); + _retrieved = true + return fetch(url) + .then(response => { + if (!response.ok) { + _retrieved = false + } + + return response.text() + }) + .then(text => { + // Load the text into the text editor & buffer + _textBUffer = text + }) + .then(() => { + // If FMD is valid, render to HTML + if (!_retrieved) { + container.innerHTML = _textBUffer + return "invalid" + } else if (isFMDValid(0, _textBUffer)) { + container.innerHTML = interpretFMD(0, _textBUffer) + return "valid" + } else { + container.innerHTML = "FMD Parsing Failed" + return "invalid" + } + }) } diff --git a/web/subject/script.js b/web/subject/script.js @@ -3,25 +3,24 @@ const go = new global.Go(); WebAssembly.instantiateStreaming(fetch("web/global/fmd.wasm"), go.importObject) - .then((result) => { + .then(async (result) => { go.run(result.instance); makeFMDState(0) _pageSubjectTitle = document.getElementById("site-page") - - _subjectView = document.getElementById("subject-view") - _subjectContent = _subjectView.getElementsByClassName("_content_")[0] + _subjectContent = document.getElementById("subject-view").getElementsByClassName("_content_")[0] _urlParams = new URLSearchParams(window.location.search); const _subject = _urlParams.get("s"); - if (_subject == "") { - _pageSubjectTitle.innerHTML = "N/A" - _subjectContent.innerHTML = "No subject specified" - } else { - _pageSubjectTitle.innerHTML = "<b>" + _subject + "</b>" - loadFMDFileHTML("https://test.wtenison.com/web/subject/" + _subject + ".fmd", _subjectContent) + + if (_subject != "") { + _res = await loadFMDFileHTML("https://test.wtenison.com/web/subject/" + _subject + ".fmd", _subjectContent) + if (_res == "valid") + _pageSubjectTitle.innerHTML = _subject + else + _pageSubjectTitle.innerHTML = "ERROR" } }) .catch(err => console.error('_ERROR:instantiateStreaming:failed to load WASM:', err)); \ No newline at end of file