Skip to content

Commit

Permalink
Merge pull request laobubu#56 from 0xGG/feat/expose-md
Browse files Browse the repository at this point in the history
feat: Exposed preview md instance
  • Loading branch information
shd101wyy authored Jul 3, 2020
2 parents 0de962e + 067d65f commit 594f1d9
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 8 deletions.
5 changes: 5 additions & 0 deletions demo/requirejs_packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var requirejs_packages = [
{ name: "markdown-it", main: "dist/markdown-it.min.js" },
{ name: "markdown-it-footnote", main: "dist/markdown-it-footnote.min.js" },
{ name: "markdown-it-emoji", main: "dist/markdown-it-emoji.min.js" },
{ name: "markdown-it-mark", main: "dist/markdown-it-mark.min.js" },
{ name: "markdown-it-ins", main: "dist/markdown-it-ins.min.js" },
{ name: "markdown-it-sub", main: "dist/markdown-it-sub.min.js" },
{ name: "markdown-it-sup", main: "dist/markdown-it-sup.min.js" },

{
name: "markdown-it-task-lists",
main: "dist/markdown-it-task-lists.min.js",
Expand Down
4 changes: 4 additions & 0 deletions dev/HyperMD.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ exports.globalNames = {
"markdown-it-footnote": "MarkdownItFootnote",
"markdown-it-emoji": "MarkdownItEmoji",
"markdown-it-task-lists": "MarkdownItTaskLists",
"markdown-it-mark": "MarkdownItMark",
"markdown-it-ins": "MarkdownItIns",
"markdown-it-sub": "MarkdownItSub",
"markdown-it-sup": "MarkdownItSup",
react: "React",
"react-dom": "ReactDOM",
vega: "vega",
Expand Down
4 changes: 4 additions & 0 deletions mode/hypermd.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
span.hmd-hidden-token {
&.cm-formatting-em,
&.cm-formatting-strong,
&.cm-formatting-mark,
&.cm-formatting-ins,
&.cm-formatting-sub,
&.cm-formatting-sup,
&.cm-formatting-strikethrough,
&.cm-formatting-code,
&.cm-formatting-link {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vickymd",
"version": "0.2.4",
"version": "0.2.5",
"description": "An experimental WYSIWYG markdown editor built on top of HyperMD with extended Widgets support",
"main": "./everything.js",
"types": "./everything.d.ts",
Expand Down Expand Up @@ -65,6 +65,10 @@
"markdown-it": "^11.0.0",
"markdown-it-emoji": "^1.4.0",
"markdown-it-footnote": "^3.0.2",
"markdown-it-ins": "^3.0.0",
"markdown-it-mark": "^3.0.0",
"markdown-it-sub": "^1.0.0",
"markdown-it-sup": "^1.0.0",
"markdown-it-task-lists": "^2.1.1",
"marked": "^1.1.0",
"mathjax": "^3.0.5",
Expand Down
4 changes: 3 additions & 1 deletion src/addon/hide-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export interface Options extends Addon.AddonOptions {
export const defaultOption: Options = {
enabled: false,
line: true,
tokenTypes: "em|strong|strikethrough|code|linkText|task".split("|"),
tokenTypes: "em|strong|mark|ins|sub|sup|strikethrough|code|linkText|task".split(
"|"
),
};

export const suggestedOption: Partial<Options> = {
Expand Down
32 changes: 32 additions & 0 deletions src/core/line-spans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface Span {
type SpanType =
| "em"
| "strong"
| "mark"
| "ins"
| "sub"
| "sup"
| "strikethrough"
| "code"
| "linkText"
Expand Down Expand Up @@ -80,6 +84,34 @@ class LineSpanExtractor {
? SpanAction.LEAVING_THIS_TYPE
: SpanAction.NOTHING,

// mark
mark: state.mark
? SpanAction.IS_THIS_TYPE
: prevState.mark
? SpanAction.LEAVING_THIS_TYPE
: SpanAction.NOTHING,

// ins
ins: state.ins
? SpanAction.IS_THIS_TYPE
: prevState.ins
? SpanAction.LEAVING_THIS_TYPE
: SpanAction.NOTHING,

// sub
sub: state.sub
? SpanAction.IS_THIS_TYPE
: prevState.sub
? SpanAction.LEAVING_THIS_TYPE
: SpanAction.NOTHING,

// sup
sup: state.sup
? SpanAction.IS_THIS_TYPE
: prevState.sup
? SpanAction.LEAVING_THIS_TYPE
: SpanAction.NOTHING,

// code
code: state.code
? SpanAction.IS_THIS_TYPE
Expand Down
8 changes: 8 additions & 0 deletions src/mode/hypermd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export interface MarkdownState {
code: false | number;
em: false | string;
strong: false | string;
mark: false | string;
ins: false | string;
sub: false | string;
sup: false | string;
header: number;
setext: 0 | 1 | 2; // current line is afxHeader before ---- ======
hr: boolean;
Expand Down Expand Up @@ -595,6 +599,10 @@ CodeMirror.defineMode(

//#endregion

//#region mark, ins, sub, sup
// TODO: Implement support
//#endregion

//#region Link, BareLink, Footnote, Wikilink etc

if (stream.current() === "[" && stream.eat("[")) {
Expand Down
10 changes: 5 additions & 5 deletions src/powerpack/paste-with-turndown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import * as TurndownService from "turndown";
import { PasteConvertor, suggestedOption, defaultOption } from "../addon/paste";

export const TurndownConvertor: PasteConvertor = html => {
export const TurndownConvertor: PasteConvertor = (html) => {
// strip <a> without href
html = html.replace(/<a([^>]*)>(.*?)<\/a>/gi, (s, attrs, content) => {
if (!/href=/i.test(attrs)) return content;
Expand All @@ -23,7 +23,7 @@ export const TurndownConvertor: PasteConvertor = html => {

// maybe you don't need to convert, if there is no img/link/header...
if (
!/\<(?:hr|img|h\d|strong|em|strikethrough|table|a|b|i|del)(?:\s.*?|\/)?\>/i.test(
!/\<(?:hr|img|h\d|strong|em|mark|ins|sub|sup|strikethrough|table|a|b|i|del)(?:\s.*?|\/)?\>/i.test(
html
)
)
Expand All @@ -35,10 +35,10 @@ export const TurndownConvertor: PasteConvertor = html => {
return null;
};

export const getTurndownService = (function() {
export const getTurndownService = (function () {
var service: TurndownService = null;

return function() {
return function () {
if (!service && typeof TurndownService === "function") {
var opts = {
headingStyle: "atx",
Expand All @@ -49,7 +49,7 @@ export const getTurndownService = (function() {
emDelimiter: "*",
strongDelimiter: "**",
linkStyle: "inlined",
linkReferenceStyle: "collapsed"
linkReferenceStyle: "collapsed",
};
service = new (TurndownService as any)(opts);

Expand Down
11 changes: 10 additions & 1 deletion src/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import MarkdownIt from "markdown-it";
import MarkdownItEmoji from "markdown-it-emoji";
import MarkdownItFootnote from "markdown-it-footnote";
import MarkdownItTaskLists from "markdown-it-task-lists";
import MarkdownItMark from "markdown-it-mark";
import MarkdownItIns from "markdown-it-ins";
import MarkdownItSub from "markdown-it-sub";
import MarkdownItSup from "markdown-it-sup";

declare var YAML: typeof import("yamljs");
// import * as YAML from "yamljs";

Expand Down Expand Up @@ -38,6 +43,10 @@ const md = new MarkdownIt({
md.use(MarkdownItEmoji, { defs: EmojiDefinitions });
md.use(MarkdownItFootnote);
md.use(MarkdownItTaskLists);
md.use(MarkdownItMark);
md.use(MarkdownItIns);
md.use(MarkdownItSub);
md.use(MarkdownItSup);
TagEnhancer(md);
MathEnhancer(md);
WidgetEnhancer(md);
Expand Down Expand Up @@ -644,4 +653,4 @@ function printPreview(
});
}

export { renderMarkdown, renderPreview, printPDF, printPreview };
export { renderMarkdown, renderPreview, printPDF, printPreview, md };
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,26 @@ markdown-it-footnote@^3.0.2:
resolved "https://registry.npm.taobao.org/markdown-it-footnote/download/markdown-it-footnote-3.0.2.tgz#1575ee7a093648d4e096aa33386b058d92ac8bc1"
integrity sha1-FXXuegk2SNTglqozOGsFjZKsi8E=

markdown-it-ins@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/markdown-it-ins/download/markdown-it-ins-3.0.0.tgz#b1b56824c78dc66e52b0fc97531b317cd78d79d2"
integrity sha1-sbVoJMeNxm5SsPyXUxsxfNeNedI=

markdown-it-mark@^3.0.0:
version "3.0.0"
resolved "https://registry.npm.taobao.org/markdown-it-mark/download/markdown-it-mark-3.0.0.tgz#27c3e39ef3cc310b2dde5375082c9fa912983cda"
integrity sha1-J8PjnvPMMQst3lN1CCyfqRKYPNo=

markdown-it-sub@^1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/markdown-it-sub/download/markdown-it-sub-1.0.0.tgz#375fd6026eae7ddcb012497f6411195ea1e3afe8"
integrity sha1-N1/WAm6ufdywEkl/ZBEZXqHjr+g=

markdown-it-sup@^1.0.0:
version "1.0.0"
resolved "https://registry.npm.taobao.org/markdown-it-sup/download/markdown-it-sup-1.0.0.tgz#cb9c9ff91a5255ac08f3fd3d63286e15df0a1fc3"
integrity sha1-y5yf+RpSVawI8/09YyhuFd8KH8M=

markdown-it-task-lists@^2.1.1:
version "2.1.1"
resolved "https://registry.npm.taobao.org/markdown-it-task-lists/download/markdown-it-task-lists-2.1.1.tgz#f68f4d2ac2bad5a2c373ba93081a1a6848417088"
Expand Down

0 comments on commit 594f1d9

Please sign in to comment.