Improve Katex error handling and fix handling of large expressions - hugo - [fork] hugo port for 9front
HTML git clone git@git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
DIR commit e1e1baa1bd33181b32c58524bc21ef21da4880db
DIR parent e42263529c35d966b752690ad1bcb461b90b470d
HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date: Sun, 11 Aug 2024 20:31:17 +0200
Improve Katex error handling and fix handling of large expressions
* Make throwOnError=true the new default
* Handle JS errors as part of the RPC request/response flow
* Return a new Result type with .Err on it
This enables constructs on the form:
```handlebars
{{ with transform.ToMath "c = \\foo{a^2 + b^2}" }}
{{ with .Err }}
{{ warnf "error: %s" . }}
{{ else }}
{{ . }}
{{ end }}
{{ end }}
```
Note that the new `Result` type behaves like `template.HTML` (or a string if needed) when printed, but it will panic if in a error state.
Closes #12748
Diffstat:
M common/types/types.go | 24 ++++++++++++++++++++++++
M internal/warpc/js/common.js | 12 +++++++++++-
M internal/warpc/js/greet.bundle.js | 4 ++--
M internal/warpc/js/renderkatex.bund… | 18 +++++++++---------
M internal/warpc/js/renderkatex.js | 9 ++++++++-
M internal/warpc/katex.go | 1 -
M internal/warpc/warpc.go | 5 ++++-
M internal/warpc/wasm/greet.wasm | 0
M internal/warpc/wasm/renderkatex.wa… | 0
M tpl/internal/go_templates/htmltemp… | 3 ++-
M tpl/internal/go_templates/htmltemp… | 12 ++++++++++++
A tpl/transform/testdata/large-katex… | 374 +++++++++++++++++++++++++++++++
M tpl/transform/transform.go | 27 +++++++++++++++++++--------
M tpl/transform/transform_integratio… | 105 +++++++++++++++++++++++++++++++
14 files changed, 570 insertions(+), 24 deletions(-)
---
DIR diff --git a/common/types/types.go b/common/types/types.go
@@ -120,3 +120,27 @@ var InvocationCounter atomic.Int64
func NewBool(b bool) *bool {
return &b
}
+
+// PrintableValueProvider is implemented by types that can provide a printable value.
+type PrintableValueProvider interface {
+ PrintableValue() any
+}
+
+var _ PrintableValueProvider = Result[any]{}
+
+// Result is a generic result type.
+type Result[T any] struct {
+ // The result value.
+ Value T
+
+ // The error value.
+ Err error
+}
+
+// PrintableValue returns the value or panics if there is an error.
+func (r Result[T]) PrintableValue() any {
+ if r.Err != nil {
+ panic(r.Err)
+ }
+ return r.Value
+}
DIR diff --git a/internal/warpc/js/common.js b/internal/warpc/js/common.js
@@ -30,13 +30,23 @@ export function readInput(handle) {
currentLine = [...currentLine, ...buffer.subarray(0, bytesRead)];
+ // Check for newline. If not, we need to read more data.
+ if (!currentLine.includes(10)) {
+ continue;
+ }
+
// Split array into chunks by newline.
let i = 0;
for (let j = 0; i < currentLine.length; i++) {
if (currentLine[i] === 10) {
const chunk = currentLine.splice(j, i + 1);
const arr = new Uint8Array(chunk);
- const json = JSON.parse(new TextDecoder().decode(arr));
+ let json;
+ try {
+ json = JSON.parse(new TextDecoder().decode(arr));
+ } catch (e) {
+ throw new Error(`Error parsing JSON '${new TextDecoder().decode(arr)}' from stdin: ${e.message}`);
+ }
handle(json);
j = i + 1;
}
DIR diff --git a/internal/warpc/js/greet.bundle.js b/internal/warpc/js/greet.bundle.js
@@ -1,2 +1,2 @@
-(()=>{function i(r){let e=[],a=new Uint8Array(1024);for(;;){let n=0;try{n=Javy.IO.readSync(0,a)}catch(o){if(o.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(n<0)throw new Error("Error reading from stdin");if(n===0)break;e=[...e,...a.subarray(0,n)];let t=0;for(let o=0;t<e.length;t++)if(e[t]===10){let f=e.splice(o,t+1),s=new Uint8Array(f),u=JSON.parse(new TextDecoder().decode(s));r(u),o=t+1}e=e.slice(t)}}function d(r){let c=new TextEncoder().encode(JSON.stringify(r)+`
-`),e=new Uint8Array(c);Javy.IO.writeSync(1,e)}var l=function(r){d({header:r.header,data:{greeting:"Hello "+r.data.name+"!"}})};console.log("Greet module loaded");i(l);})();
+(()=>{function s(r){let e=[],c=new Uint8Array(1024);for(;;){let n=0;try{n=Javy.IO.readSync(0,c)}catch(o){if(o.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(n<0)throw new Error("Error reading from stdin");if(n===0)break;if(e=[...e,...c.subarray(0,n)],!e.includes(10))continue;let t=0;for(let o=0;t<e.length;t++)if(e[t]===10){let u=e.splice(o,t+1),d=new Uint8Array(u),a;try{a=JSON.parse(new TextDecoder().decode(d))}catch(l){throw new Error(`Error parsing JSON '${new TextDecoder().decode(d)}' from stdin: ${l.message}`)}r(a),o=t+1}e=e.slice(t)}}function f(r){let i=new TextEncoder().encode(JSON.stringify(r)+`
+`),e=new Uint8Array(i);Javy.IO.writeSync(1,e)}var w=function(r){f({header:r.header,data:{greeting:"Hello "+r.data.name+"!"}})};console.log("Greet module loaded");s(w);})();
DIR diff --git a/internal/warpc/js/renderkatex.bundle.js b/internal/warpc/js/renderkatex.bundle.js
@@ -1,5 +1,5 @@
-(()=>{function Vt(r){let t=[],a=new Uint8Array(1024);for(;;){let s=0;try{s=Javy.IO.readSync(0,a)}catch(h){if(h.message.includes("os error 29"))break;throw new Error("Error reading from stdin")}if(s<0)throw new Error("Error reading from stdin");if(s===0)break;t=[...t,...a.subarray(0,s)];let o=0;for(let h=0;o<t.length;o++)if(t[o]===10){let c=t.splice(h,o+1),p=new Uint8Array(c),g=JSON.parse(new TextDecoder().decode(p));r(g),h=o+1}t=t.slice(o)}}function Ut(r){let e=new TextEncoder().encode(JSON.stringify(r)+`
drkhsh.at:70 /scm/hugo/commit/e1e1baa1bd33181b32c58524bc21ef21da4880db.gph:119: line too long