From 3eff6a0e9b0bb524c2a6016670c880be131fa305 Mon Sep 17 00:00:00 2001 From: Vinoth Kannan Date: Wed, 1 Aug 2018 15:10:15 +0530 Subject: [PATCH] DEV: Export Tag class to modify methods in plugin --- .../discourse/lib/to-markdown.js.es6 | 185 +++++++++++++----- 1 file changed, 141 insertions(+), 44 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 index fa8259b7df4..5d3fb6657de 100644 --- a/app/assets/javascripts/discourse/lib/to-markdown.js.es6 +++ b/app/assets/javascripts/discourse/lib/to-markdown.js.es6 @@ -1,11 +1,15 @@ -import parseHTML from 'discourse/helpers/parse-html'; +import parseHTML from "discourse/helpers/parse-html"; -const trimLeft = text => text.replace(/^\s+/,""); -const trimRight = text => text.replace(/\s+$/,""); -const countPipes = text => (text.replace(/\\\|/,"").match(/\|/g) || []).length; -const msoListClasses = ["MsoListParagraphCxSpFirst", "MsoListParagraphCxSpMiddle", "MsoListParagraphCxSpLast"]; +const trimLeft = text => text.replace(/^\s+/, ""); +const trimRight = text => text.replace(/\s+$/, ""); +const countPipes = text => (text.replace(/\\\|/, "").match(/\|/g) || []).length; +const msoListClasses = [ + "MsoListParagraphCxSpFirst", + "MsoListParagraphCxSpMiddle", + "MsoListParagraphCxSpLast" +]; -class Tag { +export class Tag { constructor(name, prefix = "", suffix = "", inline = false) { this.name = name; this.prefix = prefix; @@ -36,8 +40,28 @@ class Tag { } static blocks() { - return ["address", "article", "aside", "dd", "div", "dl", "dt", "fieldset", "figcaption", "figure", - "footer", "form", "header", "hgroup", "hr", "main", "nav", "p", "pre", "section"]; + return [ + "address", + "article", + "aside", + "dd", + "div", + "dl", + "dt", + "fieldset", + "figcaption", + "figure", + "footer", + "form", + "header", + "hgroup", + "hr", + "main", + "nav", + "p", + "pre", + "section" + ]; } static headings() { @@ -45,7 +69,14 @@ class Tag { } static emphases() { - return [ ["b", "**"], ["strong", "**"], ["i", "*"], ["em", "*"], ["s", "~~"], ["strike", "~~"] ]; + return [ + ["b", "**"], + ["strong", "**"], + ["i", "*"], + ["em", "*"], + ["s", "~~"], + ["strike", "~~"] + ]; } static slices() { @@ -53,7 +84,21 @@ class Tag { } static trimmable() { - return [...Tag.blocks(), ...Tag.headings(), ...Tag.slices(), "li", "td", "th", "br", "hr", "blockquote", "table", "ol", "tr", "ul"]; + return [ + ...Tag.blocks(), + ...Tag.headings(), + ...Tag.slices(), + "li", + "td", + "th", + "br", + "hr", + "blockquote", + "table", + "ol", + "tr", + "ul" + ]; } static block(name, prefix, suffix) { @@ -162,7 +207,9 @@ class Tag { const height = attr.height || pAttr.height; if (width && height) { - const pipe = this.element.parentNames.includes("table") ? "\\|" : "|"; + const pipe = this.element.parentNames.includes("table") + ? "\\|" + : "|"; alt = `${alt}${pipe}${width}x${height}`; } @@ -198,9 +245,10 @@ class Tag { toMarkdown() { const text = this.element.innerMarkdown().trim(); - if(text.includes("\n")) { // Unsupported format inside Markdown table cells + if (text.includes("\n")) { + // Unsupported format inside Markdown table cells let e = this.element; - while(e = e.parent) { + while ((e = e.parent)) { if (e.name === "table") { e.tag().invalid(); break; @@ -216,12 +264,18 @@ class Tag { static li() { return class extends Tag.slice("li", "\n") { decorate(text) { - let indent = this.element.filterParentNames(["ol", "ul"]).slice(1).map(() => "\t").join(""); + let indent = this.element + .filterParentNames(["ol", "ul"]) + .slice(1) + .map(() => "\t") + .join(""); const attrs = this.element.attributes; if (msoListClasses.includes(attrs.class)) { try { - const level = parseInt(attrs.style.match(/level./)[0].replace("level", "")); + const level = parseInt( + attrs.style.match(/level./)[0].replace("level", "") + ); indent = Array(level).join("\t") + indent; } finally { if (attrs.class === "MsoListParagraphCxSpFirst") { @@ -245,13 +299,15 @@ class Tag { decorate(text) { if (this.element.parentNames.includes("pre")) { - this.prefix = '\n\n```\n'; - this.suffix = '\n```\n\n'; + this.prefix = "\n\n```\n"; + this.suffix = "\n```\n\n"; } else { this.inline = true; } - text = $('