From c0d5e066b7dc3cbbfaf2ddfb494f59db1d369ad6 Mon Sep 17 00:00:00 2001 From: ce Date: Sun, 20 Oct 2019 20:07:29 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81strikethrough?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/demo.js | 3 ++- index.html | 5 +---- src/index.js | 7 +++++-- src/lib/mdReverse.js | 6 ++++-- src/lib/plugins/strikethrough.js | 25 +++++++++++++++++++++++++ src/lib/plugins/table.js | 8 +++----- 6 files changed, 40 insertions(+), 14 deletions(-) create mode 100644 src/lib/plugins/strikethrough.js diff --git a/demo/demo.js b/demo/demo.js index 544ea3c..220212a 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -1,6 +1,7 @@ const mdReserve = new MdReverse(); -mdReserve.use(new MdReverse.TablePlugin()); +mdReserve.use(MdReverse.plugin['table']); +mdReserve.use(MdReverse.plugin['strickthrough']); const htmlArea = document.getElementById('html-area'); const mdArea = document.getElementById('markdown-area'); diff --git a/index.html b/index.html index 31d3311..0f079b0 100644 --- a/index.html +++ b/index.html @@ -9,13 +9,10 @@ const url = `${location.protocol}//${location.host}${location.pathname}`; let replaceUrl; if (/(\/index\.html\/?|\/)/g.test(url)) { - replaceUrl = url.replace(/(\/index\.html\/?|\/)$/, "/dist/index.html"); + replaceUrl = url.replace(/(\/index\.html\/?|\/)$/, "/demo/index.html"); window.location.replace(replaceUrl); } else { document.writeln('ERROR'); - document.writeln('

老哥,url地址有问题,用下面那个试一下

'); - document.writeln('下面这个'); - document.writeln('不行还有这个'); } diff --git a/src/index.js b/src/index.js index 124b807..4a31402 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,10 @@ import {CONSOLE_TYPE} from './lib/config'; -import {MdReverse, TablePlugin} from "./lib/mdReverse"; +import {MdReverse, TablePlugin, StrikethroughPlugin} from "./lib/mdReverse"; -MdReverse.TablePlugin = TablePlugin; +MdReverse.plugin = { + table: TablePlugin, + strickthrough: StrikethroughPlugin +}; const consolee = window.console; diff --git a/src/lib/mdReverse.js b/src/lib/mdReverse.js index 161787c..a44643c 100644 --- a/src/lib/mdReverse.js +++ b/src/lib/mdReverse.js @@ -1,5 +1,6 @@ import {REGEXP, EL_TYPE, TOKEN_RULE, DEFAULT_RULE, addToken} from "./config"; import {TablePlugin} from "./plugins/table"; +import {StrikethroughPlugin} from "./plugins/strikethrough"; import {Lexer} from "./lexer"; import {Parser} from "./parser"; import {VDOMTree} from "./vdomt"; @@ -36,10 +37,11 @@ function toMarkdown(htmlStr) { } function use(plugin) { - plugin.plugin(addToken, EL_TYPE, DEFAULT_RULE); + plugin(addToken, EL_TYPE, DEFAULT_RULE); return this; } export { MdReverse, - TablePlugin + TablePlugin, + StrikethroughPlugin } diff --git a/src/lib/plugins/strikethrough.js b/src/lib/plugins/strikethrough.js new file mode 100644 index 0000000..f82ca77 --- /dev/null +++ b/src/lib/plugins/strikethrough.js @@ -0,0 +1,25 @@ +import {Tools} from "../tools/tools"; + +/** + * [Strikethrough扩展语法]{ https://www.markdownguide.org/extended-syntax/#strikethrough} + * @param addToken + * @param EL_TYPE + * @constructor + */ +function StrikethroughPlugin(addToken, EL_TYPE) { + addToken('del', true, { + filterRule: { + children: [EL_TYPE['all_element']] + }, + convertRule: function (node) { + return ' ~~'; + }, + endRule: function (node) { + return `~~ `; + } + }) +} + +export { + StrikethroughPlugin +} diff --git a/src/lib/plugins/table.js b/src/lib/plugins/table.js index 7869b0e..26e3f4e 100644 --- a/src/lib/plugins/table.js +++ b/src/lib/plugins/table.js @@ -3,12 +3,10 @@ import {Tools} from "../tools/tools"; /** * [Table扩展语法]{ https://www.markdownguide.org/extended-syntax/#tables} * @param addToken + * @param EL_TYPE * @constructor */ -function TablePlugin() { -} - -TablePlugin.prototype.plugin = function (addToken, EL_TYPE) { +function TablePlugin(addToken, EL_TYPE) { addToken('td', true, { filterRule: { children: [EL_TYPE['all_element']] @@ -96,7 +94,7 @@ TablePlugin.prototype.plugin = function (addToken, EL_TYPE) { return ''; } }); -}; +} export { TablePlugin