diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-01-25 18:34:35 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-01-25 18:34:35 +0100 |
commit | 11261345744b06d81f1aadc33f288100265ecd2f (patch) | |
tree | 8078f15a4356f043651c90ed09c4af6dd4050c0c /anki-card-template/card.js | |
parent | 71fc52b7d8d920ba1b0b11645064545e4c85551b (diff) |
better separator on vertical mobile + better definition separator parser2.0.0
Diffstat (limited to 'anki-card-template/card.js')
-rw-r--r-- | anki-card-template/card.js | 48 |
1 files changed, 41 insertions, 7 deletions
diff --git a/anki-card-template/card.js b/anki-card-template/card.js index a2d12c4..2b04ca3 100644 --- a/anki-card-template/card.js +++ b/anki-card-template/card.js @@ -192,13 +192,47 @@ function parseTags(nodes) { } function parseDefinitions(nodes) { - out = `<ul class="definitions">`; - out += nodes.map(n => n.data).join("").split(",") - .map(s => s.trim()) - .map(s => s.replace(/{(.+)}/g, `<span class="subtile">$1</span>`)) // {note} - .map(s => `<li class="definition">${s}</li>`) - .join(`<li class="definition-separator"></li>`); - out += `</ul>`; + out = `<ul class="definitions"><li class="definition">`; + var subtile = false; // current text is subtile + var parenthesis = false; // current text is surrounded by parenthesis + + for (var node of nodes) { + if (node.type == "html") { + out += node.data; + continue; + } + + var input = node.data; + for (var i = 0; i < input.length; i++) { + // escape characters preceded by \ + if (input[i] == "\\") { + var escaped = input[i+1]; + if (escaped == "{") { out += "{"; i++; continue; } + if (escaped == "}") { out += "}"; i++; continue; } + if (escaped == ",") { out += ","; i++; continue; } + if (escaped == "(") { out += "("; i++; continue; } + if (escaped == ")") { out += ")"; i++; continue; } + } + + // subtile brackets + if (input[i] == "{") { subtile = true; out += `<span class="subtile">`; continue; } + if (input[i] == "}" && subtile) { subtile = false; out += `</span>`; continue; } + + // definition separator + if (input[i] == "," && !subtile && !parenthesis) { + out += `</li><li class="definition-separator"></li><li class="definition">`; + continue; + } + + // ignore comma's starting new definition in parenthesis + if (input[i] == "(") parenthesis = true; + if (input[i] == ")") parenthesis = false; + + out += input[i]; + } + } + + out += `</li></ul>`; return HTMLtoParseArr(out); } |