- Ricerca nel testo: la cartella viene percorsa ricorsivamente e le note .md scaricate una alla volta (il WebDAV non cerca nel contenuto), con avanzamento, possibilità di fermare e il passaggio che contiene la parola. La ricerca per titolo nella lista resta istantanea. - Caselle di spunta: "- [ ]" e "- [x]" diventano caselle toccabili nell'anteprima; il tocco riscrive quella sola riga e salva subito, e l'editor segue (senza, uscendo riscriverebbe la versione vecchia sul server). - Tag: front-matter (tags: [a, b], lista a trattini o separata da virgole) e #tag nel testo; pastiglie in cima all'anteprima, #tag toccabili, nuova schermata Tag con il conteggio per tag. Il blocco front-matter non viene più stampato come testo. - tests/qml_contract_check.py: confronta i nomi JS e le proprietà usati dalla QML con i sorgenti, le pagine raggiungibili e i parametri dei gestori dei segnali — la classe di errori che la compilazione non vede. - Catalogo italiano aggiornato (25 stringhe nuove); repo ripulito dai residui di build e .gitignore esteso.
420 lines
16 KiB
JavaScript
420 lines
16 KiB
JavaScript
// Markdown -> HTML subset (Qt rich text) renderer. Pure ES5, no deps.
|
|
// IMPORTANT (QML): functions are TOP-LEVEL so a QML namespace import exposes
|
|
// them directly: import "../components/Markdown.js" as Markdown
|
|
// ... Markdown.render(src) ...
|
|
// (an IIFE + var would hide them under Markdown.Markdown and QML would throw
|
|
// "Property 'render' of object [object Object] is not a function").
|
|
// The module.exports guard at the bottom is for node tests only.
|
|
//
|
|
// render(src, baseUrl, opts):
|
|
// baseUrl - resolves relative links/images against (the note's WebDAV folder)
|
|
// opts.imageMap - { url: localCachePath } -> inline <img> instead of placeholder
|
|
// opts.imageLabel - localized word for the image placeholder ("image")
|
|
// opts.checkOff / opts.checkOn - colours of the task checkbox glyphs
|
|
//
|
|
// Notes on the extra syntax supported here:
|
|
// - a leading front-matter block (--- ... ---) is NOT rendered as text: the
|
|
// page shows its tags as chips, so the block is skipped, not printed
|
|
// - task list items (- [ ] / - [x]) become tappable checkboxes: the glyph is
|
|
// an <a> whose href carries the SOURCE LINE NUMBER (quill-check://12), so
|
|
// the preview can rewrite exactly that line on the server
|
|
// - #tags become quill-tag:// links (the app opens a tag search)
|
|
|
|
function escapeHtml(s) {
|
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
}
|
|
|
|
function resolveUrl(u, baseUrl) {
|
|
if (baseUrl && !/^[a-z]+:/i.test(u) && u.charAt(0) !== "#" && u.charAt(0) !== "/") {
|
|
return String(baseUrl).replace(/\/+$/, "") + "/" + u;
|
|
}
|
|
return u;
|
|
}
|
|
|
|
// ── front matter ───────────────────────────────────────────────────────────
|
|
// Only the subset that matters to a notes client is understood: a "key: value"
|
|
// map where a value can be a scalar, an inline list ([a, b]) or a block list
|
|
// (dash lines). Everything else is kept as text in fields[] but not interpreted.
|
|
|
|
function addTagValues(raw, out) {
|
|
if (!raw) return;
|
|
var v = String(raw).trim().replace(/^["']|["']$/g, "");
|
|
if (v.charAt(0) === "[") v = v.substring(1);
|
|
if (v.charAt(v.length - 1) === "]") v = v.substring(0, v.length - 1);
|
|
var parts = v.split(",");
|
|
for (var i = 0; i < parts.length; i++) {
|
|
var t = parts[i].trim().replace(/^#/, "");
|
|
if (t) out.push(t);
|
|
}
|
|
}
|
|
|
|
function frontMatterInfo(src) {
|
|
var info = { present: false, bodyStart: 0, tags: [], fields: {} };
|
|
if (!src) return info;
|
|
var text = String(src).replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/^\uFEFF/, "");
|
|
var lines = text.split("\n");
|
|
if (lines.length < 2 || lines[0].trim() !== "---") return info;
|
|
var end = -1;
|
|
for (var i = 1; i < lines.length; i++) {
|
|
if (lines[i].trim() === "---" || lines[i].trim() === "...") { end = i; break; }
|
|
}
|
|
if (end < 0) return info; // unterminated: treat the dashes as normal text
|
|
|
|
info.present = true;
|
|
info.bodyStart = end + 1;
|
|
|
|
var key = null;
|
|
var values = {};
|
|
for (var j = 1; j < end; j++) {
|
|
var line = lines[j];
|
|
var item = /^\s*-\s+(.+)$/.exec(line);
|
|
if (item && key) {
|
|
values[key].push(item[1].trim());
|
|
continue;
|
|
}
|
|
var kv = /^([A-Za-z0-9_.-]+)\s*:\s*(.*)$/.exec(line);
|
|
if (!kv) continue;
|
|
key = kv[1].toLowerCase();
|
|
values[key] = [];
|
|
var v = kv[2].trim();
|
|
if (v) values[key].push(v);
|
|
}
|
|
|
|
for (var k in values) {
|
|
if (values.hasOwnProperty(k)) info.fields[k] = values[k].join(", ");
|
|
}
|
|
var tagValues = values.tags || values.tag || [];
|
|
for (var t = 0; t < tagValues.length; t++) addTagValues(tagValues[t], info.tags);
|
|
|
|
return info;
|
|
}
|
|
|
|
// Tags of a note: front matter first, then inline #tags in the body (code
|
|
// fences skipped, because a "#" there is a comment and not a tag).
|
|
// Deduplicated case-insensitively, original case kept.
|
|
function tagDetails(src) {
|
|
var out = { frontMatter: [], inline: [] };
|
|
if (!src) return out;
|
|
var fm = frontMatterInfo(src);
|
|
var seen = {};
|
|
|
|
function add(tag, bucket) {
|
|
var t = String(tag).replace(/^#/, "").trim();
|
|
if (!t || /^[0-9]+$/.test(t)) return;
|
|
var k = t.toLowerCase();
|
|
if (seen[k]) return;
|
|
seen[k] = true;
|
|
bucket.push(t);
|
|
}
|
|
|
|
for (var f = 0; f < fm.tags.length; f++) add(fm.tags[f], out.frontMatter);
|
|
|
|
var text = String(src).replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/^\uFEFF/, "");
|
|
var lines = text.split("\n");
|
|
var fence = false;
|
|
var re = /(^|[\s(])#([0-9A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF_\/-]{2,})/g;
|
|
for (var i = fm.bodyStart; i < lines.length; i++) {
|
|
var l = lines[i];
|
|
if (/^\s*(```|~~~)/.test(l)) { fence = !fence; continue; }
|
|
if (fence) continue;
|
|
re.lastIndex = 0;
|
|
var m;
|
|
while ((m = re.exec(l)) !== null) add(m[2], out.inline);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
function hasTag(src, tag) {
|
|
if (!tag) return false;
|
|
var d = tagDetails(src);
|
|
var want = String(tag).replace(/^#/, "").toLowerCase();
|
|
var all = d.frontMatter.concat(d.inline);
|
|
for (var i = 0; i < all.length; i++) {
|
|
if (all[i].toLowerCase() === want) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// ── task lists ─────────────────────────────────────────────────────────────
|
|
// Flip one task marker, keeping the file's line endings. Returns the new source
|
|
// or null when the line is not a task item.
|
|
function toggleTask(src, lineIndex) {
|
|
if (!src) return null;
|
|
var text = String(src);
|
|
var crlf = text.indexOf("\r\n") >= 0;
|
|
var lines = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
|
|
if (lineIndex < 0 || lineIndex >= lines.length) return null;
|
|
var re = /^(\s*(?:[-*+]|\d+[.)])\s+\[)([ xX])(\])([\s\S]*)$/;
|
|
var m = re.exec(lines[lineIndex]);
|
|
if (!m) return null;
|
|
var next = m[2].toLowerCase() === "x" ? " " : "x";
|
|
lines[lineIndex] = m[1] + next + m[3] + m[4];
|
|
return lines.join(crlf ? "\r\n" : "\n");
|
|
}
|
|
|
|
// ── text search ────────────────────────────────────────────────────────────
|
|
// Plain-text (not markdown-aware) search: the query is what the reader typed,
|
|
// so it must match what is written in the file. Returns the offset and a
|
|
// readable excerpt around the first hit.
|
|
function snippetAround(text, at, len) {
|
|
var start = at - 40;
|
|
if (start < 0) start = 0;
|
|
var end = at + len + 60;
|
|
if (end > text.length) end = text.length;
|
|
var s = text.substring(start, end).replace(/\s+/g, " ");
|
|
// Drop the markdown noise that would otherwise open the excerpt
|
|
// ("- [x] ", "> ", "| ", "### ") and the inline markers inside it.
|
|
s = s.replace(/^[\s\-*+>#|]+/, "").replace(/\s*\|\s*/g, " · ");
|
|
s = s.replace(/\*\*|__|`|\[\[|\]\]/g, "");
|
|
// A cut in the middle of a word is worse than a shorter excerpt.
|
|
if (start > 0) s = s.replace(/^\S*\s+/, "");
|
|
return (start > 0 ? "\u2026" : "") + s.trim() + (end < text.length ? "\u2026" : "");
|
|
}
|
|
|
|
function searchText(content, term) {
|
|
if (!content || !term) return null;
|
|
var hay = String(content).toLowerCase();
|
|
var needle = String(term).toLowerCase();
|
|
var at = hay.indexOf(needle);
|
|
if (at < 0) return null;
|
|
return { index: at, snippet: snippetAround(String(content), at, needle.length) };
|
|
}
|
|
|
|
// ── inline formatting ──────────────────────────────────────────────────────
|
|
|
|
// #tag -> quill-tag://<tag>. Applied only OUTSIDE existing anchors: a tag typed
|
|
// inside a link label must not produce a link inside a link.
|
|
function linkTags(html) {
|
|
var parts = String(html).split(/(<a\b[^>]*>[\s\S]*?<\/a>)/);
|
|
var re = /(^|[\s(])#([0-9A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF_\/-]{2,})/g;
|
|
for (var p = 0; p < parts.length; p++) {
|
|
if (/^<a\b/.test(parts[p])) continue;
|
|
parts[p] = parts[p].replace(re, function (m, pre, tag) {
|
|
if (/^[0-9]+$/.test(tag)) return m;
|
|
return pre + '<a href="quill-tag://' + encodeURIComponent(tag) + '">#' + tag + "</a>";
|
|
});
|
|
}
|
|
return parts.join("");
|
|
}
|
|
|
|
function inline(s, baseUrl, opts) {
|
|
s = escapeHtml(s);
|
|
s = s.replace(/`([^`]+)`/g, "<tt>$1</tt>");
|
|
s = s.replace(/!\[([^\]]*)\]\(([^)\s]+)\)/g, function (m, alt, u) {
|
|
var key = resolveUrl(u, baseUrl);
|
|
var p = opts && opts.imageMap ? opts.imageMap[key] : "";
|
|
if (p && p !== "error") {
|
|
return '<img src="file://' + p + '" width="350" height="-1" alt="' + alt + '"/>';
|
|
}
|
|
var label = opts && opts.imageLabel ? opts.imageLabel : "image";
|
|
return "<i>[" + label + ": " + alt + "]</i>";
|
|
});
|
|
s = s.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, function (m, t, u) {
|
|
return '<a href="' + resolveUrl(u, baseUrl) + '">' + t + "</a>";
|
|
});
|
|
// Wiki links [[Nota]] -> quill://<url-encoded name.md>; the app intercepts
|
|
// the scheme and opens the note in the editor.
|
|
s = s.replace(/\[\[([^\]]+)\]\]/g, function (m, name) {
|
|
var full = name;
|
|
var lower = name.toLowerCase();
|
|
if (lower.indexOf(".md") !== lower.length - 3) full = name + ".md";
|
|
var label = full;
|
|
if (label.toLowerCase().indexOf(".md") === label.length - 3) {
|
|
label = label.substring(0, label.length - 3);
|
|
}
|
|
return '<a href="quill://' + encodeURIComponent(full) + '">' + label + "</a>";
|
|
});
|
|
s = s.replace(/\*\*(.+?)\*\*/g, "<b>$1</b>");
|
|
s = s.replace(/(^|[^*])\*([^*\n]+)\*(?!\*)/g, "$1<i>$2</i>");
|
|
s = s.replace(/~~([^~]+)~~/g, "<s>$1</s>");
|
|
return linkTags(s);
|
|
}
|
|
|
|
// Split a table row on unescaped pipes; leading/trailing pipe is optional.
|
|
function splitRow(s) {
|
|
var t = s.trim();
|
|
if (t.charAt(0) === "|") t = t.substring(1);
|
|
if (t.charAt(t.length - 1) === "|") t = t.substring(0, t.length - 1);
|
|
var parts = t.split("|");
|
|
var cells = [];
|
|
for (var i = 0; i < parts.length; i++) cells.push(parts[i].trim());
|
|
return cells;
|
|
}
|
|
|
|
// GFM alignment from a delimiter cell: ":---" left, "---:" right, ":---:" center.
|
|
function alignmentFromSep(sep, n) {
|
|
var parts = splitRow(sep);
|
|
var aligns = [];
|
|
for (var i = 0; i < n; i++) {
|
|
var p = i < parts.length ? parts[i] : "";
|
|
if (p.charAt(0) === ":" && p.charAt(p.length - 1) === ":") aligns.push("center");
|
|
else if (p.charAt(0) === ":") aligns.push("left");
|
|
else if (p.charAt(p.length - 1) === ":") aligns.push("right");
|
|
else aligns.push("");
|
|
}
|
|
return aligns;
|
|
}
|
|
|
|
function alignAttr(a) {
|
|
return a ? ' align="' + a + '"' : "";
|
|
}
|
|
|
|
function render(src, baseUrl, opts) {
|
|
if (!src) return "";
|
|
src = src.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
// Strip UTF-8 BOM: a leading \uFEFF breaks the first heading/block
|
|
// regex (line no longer starts with #) and the symbol stays visible.
|
|
src = src.replace(/^\uFEFF/, "");
|
|
var lines = src.split("\n");
|
|
var out = [];
|
|
var fm = frontMatterInfo(src);
|
|
// Front matter is shown by the page as tag chips, never as body text.
|
|
var i = fm.bodyStart;
|
|
var n = lines.length;
|
|
|
|
var checkOff = (opts && opts.checkOff) || "#7f7f7f";
|
|
var checkOn = (opts && opts.checkOn) || "#3b9c9c";
|
|
|
|
var headingRe = /^\s*(#{1,6})\s+(.*)$/;
|
|
var hrRe = /^\s*(-{3,}|\*{3,}|_{3,})\s*$/;
|
|
var quoteRe = /^>\s?(.*)$/;
|
|
var listRe = /^\s*([-*+]|\d+[.)])\s+(.*)$/;
|
|
var taskRe = /^\s*(?:[-*+]|\d+[.)])\s+\[([ xX])\]\s*([\s\S]*)$/;
|
|
var fenceRe = /^```/;
|
|
var tableHeaderRe = /^\s*\|.*\|/;
|
|
var tableSepRe = /^\s*\|?[\s:|-]+\|?\s*$/;
|
|
|
|
while (i < n) {
|
|
var line = lines[i];
|
|
var m;
|
|
|
|
if (fenceRe.test(line)) {
|
|
var buf = [];
|
|
i++;
|
|
while (i < n && !fenceRe.test(lines[i])) {
|
|
buf.push(lines[i]);
|
|
i++;
|
|
}
|
|
i++; // closing fence
|
|
out.push('<pre style="color:#9a9a9a">' + escapeHtml(buf.join("\n")) + "</pre>");
|
|
continue;
|
|
}
|
|
|
|
// GFM table: header row + delimiter row (must contain a pipe and a dash).
|
|
if (tableHeaderRe.test(line) && i + 1 < n) {
|
|
var sepLine = lines[i + 1];
|
|
if (tableSepRe.test(sepLine) && sepLine.indexOf("|") >= 0 && sepLine.indexOf("-") >= 0) {
|
|
var headerCells = splitRow(line);
|
|
var aligns = alignmentFromSep(sepLine, headerCells.length);
|
|
var tbody = [];
|
|
i += 2;
|
|
while (i < n) {
|
|
var dl = lines[i];
|
|
if (dl.trim() === "" || dl.indexOf("|") < 0) break;
|
|
tbody.push(splitRow(dl));
|
|
i++;
|
|
}
|
|
var t = '<table border="1" cellspacing="0" cellpadding="4">';
|
|
t += "<tr>";
|
|
for (var h = 0; h < headerCells.length; h++) {
|
|
t += "<th" + alignAttr(aligns[h]) + "><b>" + inline(headerCells[h], baseUrl, opts) + "</b></th>";
|
|
}
|
|
t += "</tr>";
|
|
for (var r = 0; r < tbody.length; r++) {
|
|
t += "<tr>";
|
|
var row = tbody[r];
|
|
for (var c = 0; c < headerCells.length; c++) {
|
|
var cell = c < row.length ? row[c] : "";
|
|
t += "<td" + alignAttr(aligns[c]) + ">" + inline(cell, baseUrl, opts) + "</td>";
|
|
}
|
|
t += "</tr>";
|
|
}
|
|
t += "</table>";
|
|
out.push(t);
|
|
continue;
|
|
}
|
|
}
|
|
|
|
m = headingRe.exec(line);
|
|
if (m) {
|
|
var level = m[1].length;
|
|
out.push("<h" + level + ">" + inline(m[2], baseUrl, opts) + "</h" + level + ">");
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
if (hrRe.test(line)) {
|
|
out.push("<hr/>");
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
m = quoteRe.exec(line);
|
|
if (m) {
|
|
var q = [];
|
|
while (i < n) {
|
|
var qm = quoteRe.exec(lines[i]);
|
|
if (!qm) break;
|
|
q.push(inline(qm[1], baseUrl, opts));
|
|
i++;
|
|
}
|
|
out.push('<p style="color:#8f8f8f"><i>' + q.join("<br/>") + "</i></p>");
|
|
continue;
|
|
}
|
|
|
|
m = listRe.exec(line);
|
|
if (m) {
|
|
var ordered = /^\s*\d/.test(line);
|
|
var items = [];
|
|
while (i < n) {
|
|
var lm = listRe.exec(lines[i]);
|
|
if (!lm) break;
|
|
var task = taskRe.exec(lines[i]);
|
|
if (task) {
|
|
var done = task[1].toLowerCase() === "x";
|
|
var glyph = done ? "\u2611" : "\u2610";
|
|
var colour = done ? checkOn : checkOff;
|
|
// The href carries the absolute source line: the preview
|
|
// rewrites that line, so it must not drift with offsets.
|
|
items.push('<li><a href="quill-check://' + i + '"><font color="' + colour + '">' +
|
|
glyph + "</font></a> " + inline(task[2], baseUrl, opts) + "</li>");
|
|
} else {
|
|
items.push("<li>" + inline(lm[2], baseUrl, opts) + "</li>");
|
|
}
|
|
i++;
|
|
}
|
|
out.push(ordered ? "<ol>" + items.join("") + "</ol>" : "<ul>" + items.join("") + "</ul>");
|
|
continue;
|
|
}
|
|
|
|
if (line.trim() === "") {
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
var para = [];
|
|
while (i < n) {
|
|
var pl = lines[i];
|
|
if (pl.trim() === "" || headingRe.test(pl) || hrRe.test(pl) ||
|
|
quoteRe.test(pl) || listRe.test(pl) || fenceRe.test(pl)) {
|
|
break;
|
|
}
|
|
para.push(inline(pl, baseUrl, opts));
|
|
i++;
|
|
}
|
|
out.push("<p>" + para.join("<br/>") + "</p>");
|
|
}
|
|
return out.join("\n");
|
|
}
|
|
|
|
if (typeof module !== "undefined" && module.exports) {
|
|
module.exports = {
|
|
render: render,
|
|
frontMatterInfo: frontMatterInfo,
|
|
tagDetails: tagDetails,
|
|
hasTag: hasTag,
|
|
toggleTask: toggleTask,
|
|
searchText: searchText
|
|
};
|
|
}
|