From a160bdf4df5d2f870778e5ac22a623274f74c9f5 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 13 Oct 2019 14:04:54 +0300 Subject: [PATCH 1/4] Persist code block language when editing Signed-off-by: Tulir Asokan --- src/editor/deserialize.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/editor/deserialize.js b/src/editor/deserialize.js index d41e413dbc..abcfbf7dd7 100644 --- a/src/editor/deserialize.js +++ b/src/editor/deserialize.js @@ -58,7 +58,16 @@ function parseLink(a, partCreator) { function parseCodeBlock(n, partCreator) { const parts = []; - const preLines = ("```\n" + n.textContent + "```").split("\n"); + let language = ""; + if (n.firstChild && n.firstChild.nodeName === "CODE") { + for (const className of n.firstChild.classList) { + if (className.startsWith("language-")) { + language = className.substr("language-".length); + break; + } + } + } + const preLines = ("```" + language + "\n" + n.textContent + "```").split("\n"); preLines.forEach((l, i) => { parts.push(partCreator.plain(l)); if (i < preLines.length - 1) { From 29367766fd107b1209a1d3965a33df7dbcc081f4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 13 Oct 2019 14:10:11 +0300 Subject: [PATCH 2/4] Fix "decend" typo Signed-off-by: Tulir Asokan --- src/editor/deserialize.js | 16 ++++++++-------- src/editor/dom.js | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/editor/deserialize.js b/src/editor/deserialize.js index abcfbf7dd7..58e188457a 100644 --- a/src/editor/deserialize.js +++ b/src/editor/deserialize.js @@ -124,19 +124,19 @@ function parseElement(n, partCreator, lastNode, state) { state.listDepth = (state.listDepth || 0) + 1; // es-lint-disable-next-line no-fallthrough default: - // don't textify block nodes we'll decend into - if (!checkDecendInto(n)) { + // don't textify block nodes we'll descend into + if (!checkDescendInto(n)) { return partCreator.plain(n.textContent); } } } -function checkDecendInto(node) { +function checkDescendInto(node) { switch (node.nodeName) { case "PRE": // a code block is textified in parseCodeBlock // as we don't want to preserve markup in it, - // so no need to decend into it + // so no need to descend into it return false; default: return checkBlockNode(node); @@ -212,11 +212,11 @@ function parseHtmlMessage(html, partCreator, isQuotedMessage) { parts.push(...newParts); - const decend = checkDecendInto(n); - // when not decending (like for PRE), onNodeLeave won't be called to set lastNode + const descend = checkDescendInto(n); + // when not descending (like for PRE), onNodeLeave won't be called to set lastNode // so do that here. - lastNode = decend ? null : n; - return decend; + lastNode = descend ? null : n; + return descend; } function onNodeLeave(n) { diff --git a/src/editor/dom.js b/src/editor/dom.js index e82c3f70ca..3efc64f1c9 100644 --- a/src/editor/dom.js +++ b/src/editor/dom.js @@ -21,8 +21,8 @@ import DocumentOffset from "./offset"; export function walkDOMDepthFirst(rootNode, enterNodeCallback, leaveNodeCallback) { let node = rootNode.firstChild; while (node && node !== rootNode) { - const shouldDecend = enterNodeCallback(node); - if (shouldDecend && node.firstChild) { + const shouldDescend = enterNodeCallback(node); + if (shouldDescend && node.firstChild) { node = node.firstChild; } else if (node.nextSibling) { node = node.nextSibling; From a95f7be22d67a98a7c1df074d12d147090a70899 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 13 Oct 2019 14:27:12 +0300 Subject: [PATCH 3/4] Persist list indexes when editing Signed-off-by: Tulir Asokan --- src/editor/deserialize.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/editor/deserialize.js b/src/editor/deserialize.js index 58e188457a..2662d5a503 100644 --- a/src/editor/deserialize.js +++ b/src/editor/deserialize.js @@ -108,7 +108,9 @@ function parseElement(n, partCreator, lastNode, state) { case "LI": { const indent = " ".repeat(state.listDepth - 1); if (n.parentElement.nodeName === "OL") { - return partCreator.plain(`${indent}1. `); + // The markdown parser doesn't do nested indexed lists at all, but this supports it anyway. + let index = state.listIndex[state.listIndex.length - 1]++; + return partCreator.plain(`${indent}${index}. `); } else { return partCreator.plain(`${indent}- `); } @@ -120,9 +122,11 @@ function parseElement(n, partCreator, lastNode, state) { break; } case "OL": + state.listIndex.push(n.start || 1); + // fallthrough case "UL": state.listDepth = (state.listDepth || 0) + 1; - // es-lint-disable-next-line no-fallthrough + // fallthrough default: // don't textify block nodes we'll descend into if (!checkDescendInto(n)) { @@ -177,7 +181,9 @@ function parseHtmlMessage(html, partCreator, isQuotedMessage) { const parts = []; let lastNode; let inQuote = isQuotedMessage; - const state = {}; + const state = { + listIndex: [], + }; function onNodeEnter(n) { if (checkIgnored(n)) { @@ -228,6 +234,8 @@ function parseHtmlMessage(html, partCreator, isQuotedMessage) { inQuote = false; break; case "OL": + state.listIndex.pop(); + // fallthrough case "UL": state.listDepth -= 1; break; From 75bcc3f84913c61960b9734cd60929df3d4e369b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 18 Oct 2019 19:58:55 +0300 Subject: [PATCH 4/4] Update src/editor/deserialize.js Co-Authored-By: Bruno Windels --- src/editor/deserialize.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/editor/deserialize.js b/src/editor/deserialize.js index 2662d5a503..6636c9971e 100644 --- a/src/editor/deserialize.js +++ b/src/editor/deserialize.js @@ -109,7 +109,8 @@ function parseElement(n, partCreator, lastNode, state) { const indent = " ".repeat(state.listDepth - 1); if (n.parentElement.nodeName === "OL") { // The markdown parser doesn't do nested indexed lists at all, but this supports it anyway. - let index = state.listIndex[state.listIndex.length - 1]++; + let index = state.listIndex[state.listIndex.length - 1]; + state.listIndex[state.listIndex.length - 1] += 1; return partCreator.plain(`${indent}${index}. `); } else { return partCreator.plain(`${indent}- `);