From 88419529f804c5e0e3770ef9a911cf243fbf292d Mon Sep 17 00:00:00 2001
From: yanglbme
Date: Thu, 26 Nov 2020 20:10:39 +0800
Subject: [PATCH] style: update wx-renderer
---
package-lock.json | 2 +-
package.json | 2 +-
src/assets/scripts/renderers/wx-renderer.js | 350 ++++++++++----------
3 files changed, 181 insertions(+), 173 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index f498dfa..3dbe169 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "vue-md",
- "version": "1.4.4",
+ "version": "1.4.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 311013f..229115f 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "vue-md",
- "version": "1.4.4",
+ "version": "1.4.5",
"homepage": ".",
"description": "An open-source wechat markdown editor.",
"author": "doocs",
diff --git a/src/assets/scripts/renderers/wx-renderer.js b/src/assets/scripts/renderers/wx-renderer.js
index f489d59..1e6d1e0 100644
--- a/src/assets/scripts/renderers/wx-renderer.js
+++ b/src/assets/scripts/renderers/wx-renderer.js
@@ -1,72 +1,73 @@
import marked from "marked";
-const WxRenderer = function (opts) {
- this.opts = opts;
- let ENV_STRETCH_IMAGE = true;
+class WxRenderer {
+ constructor(opts) {
+ this.opts = opts;
+ let ENV_STRETCH_IMAGE = true;
- let footnotes = [];
- let footnoteIndex = 0;
- let styleMapping = null;
+ let footnotes = [];
+ let footnoteIndex = 0;
+ let styleMapping = null;
- const CODE_FONT_FAMILY =
- "Menlo, Operator Mono, Consolas, Monaco, monospace";
+ const CODE_FONT_FAMILY =
+ "Menlo, Operator Mono, Consolas, Monaco, monospace";
- let merge = (base, extend) => Object.assign({}, base, extend);
+ let merge = (base, extend) => Object.assign({}, base, extend);
- this.buildTheme = (themeTpl) => {
- let mapping = {};
- let base = merge(themeTpl.BASE, {
- "font-family": this.opts.fonts,
- "font-size": this.opts.size,
- });
- let base_block = merge(base, {});
- for (let ele in themeTpl.inline) {
- if (themeTpl.inline.hasOwnProperty(ele)) {
- let style = themeTpl.inline[ele];
- mapping[ele] = merge(base, style);
- }
- }
-
- for (let ele in themeTpl.block) {
- if (themeTpl.block.hasOwnProperty(ele)) {
- let style = themeTpl.block[ele];
- if (ele === "code") {
- style["font-family"] = CODE_FONT_FAMILY;
+ this.buildTheme = (themeTpl) => {
+ let mapping = {};
+ let base = merge(themeTpl.BASE, {
+ "font-family": this.opts.fonts,
+ "font-size": this.opts.size,
+ });
+ let base_block = merge(base, {});
+ for (let ele in themeTpl.inline) {
+ if (themeTpl.inline.hasOwnProperty(ele)) {
+ let style = themeTpl.inline[ele];
+ mapping[ele] = merge(base, style);
}
- mapping[ele] = merge(base_block, style);
}
- }
- return mapping;
- };
- let getStyles = (tokenName, addition) => {
- let arr = [];
- let dict = styleMapping[tokenName];
- if (!dict) return "";
- for (const key in dict) {
- arr.push(key + ":" + dict[key]);
- }
- return `style="${arr.join(";") + (addition || "")}"`;
- };
-
- let addFootnote = (title, link) => {
- footnotes.push([++footnoteIndex, title, link]);
- return footnoteIndex;
- };
-
- this.buildFootnotes = () => {
- let footnoteArray = footnotes.map((x) => {
- if (x[1] === x[2]) {
- return `[${x[0]}]
: ${x[1]}
`;
+ for (let ele in themeTpl.block) {
+ if (themeTpl.block.hasOwnProperty(ele)) {
+ let style = themeTpl.block[ele];
+ if (ele === "code") {
+ style["font-family"] = CODE_FONT_FAMILY;
+ }
+ mapping[ele] = merge(base_block, style);
+ }
}
- return `[${x[0]}]
${x[1]}: ${x[2]}
`;
- });
- return `引用链接
${footnoteArray.join("\n")}
`;
- };
+ return mapping;
+ };
- this.buildAddition = () => {
- return `
+ let getStyles = (tokenName, addition) => {
+ let arr = [];
+ let dict = styleMapping[tokenName];
+ if (!dict) return "";
+ for (const key in dict) {
+ arr.push(key + ":" + dict[key]);
+ }
+ return `style="${arr.join(";") + (addition || "")}"`;
+ };
+
+ let addFootnote = (title, link) => {
+ footnotes.push([++footnoteIndex, title, link]);
+ return footnoteIndex;
+ };
+
+ this.buildFootnotes = () => {
+ let footnoteArray = footnotes.map((x) => {
+ if (x[1] === x[2]) {
+ return `[${x[0]}]
: ${x[1]}
`;
+ }
+ return `[${x[0]}]
${x[1]}: ${x[2]}
`;
+ });
+ return `引用链接
${footnoteArray.join("\n")}
`;
+ };
+
+ this.buildAddition = () => {
+ return `
`;
- };
-
- this.setOptions = (newOpts) => {
- this.opts = merge(this.opts, newOpts);
- };
-
- this.hasFootnotes = () => footnotes.length !== 0;
-
- this.getRenderer = (status) => {
- footnotes = [];
- footnoteIndex = 0;
-
- styleMapping = this.buildTheme(this.opts.theme);
- let renderer = new marked.Renderer();
-
- renderer.heading = (text, level) => {
- switch (level) {
- case 1:
- return `${text}
`;
- case 2:
- return `${text}
`;
- case 3:
- return `${text}
`;
- default:
- return `${text}
`;
- }
- };
- renderer.paragraph = (text) => {
- if (text.indexOf("
`;
};
- renderer.blockquote = (text) => {
- text = text.replace(//g, ``);
- return `
${text}
`;
+ this.setOptions = (newOpts) => {
+ this.opts = merge(this.opts, newOpts);
};
- renderer.code = (text, lang) => {
- text = text.replace(//g, ">");
- const codeLines = text
- .split("\n")
- .map(
- (line) =>
- `${
- line || "
"
- }
`
+
+ this.hasFootnotes = () => footnotes.length !== 0;
+
+ this.getRenderer = (status) => {
+ footnotes = [];
+ footnoteIndex = 0;
+
+ styleMapping = this.buildTheme(this.opts.theme);
+ let renderer = new marked.Renderer();
+
+ renderer.heading = (text, level) => {
+ switch (level) {
+ case 1:
+ return `${text}
`;
+ case 2:
+ return `${text}
`;
+ case 3:
+ return `${text}
`;
+ default:
+ return `${text}
`;
+ }
+ };
+ renderer.paragraph = (text) => {
+ if (
+ text.indexOf("