Skip to content

Commit

Permalink
🎨 #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Mar 13, 2019
1 parent ab4c6ea commit 138d10e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 8 deletions.
4 changes: 3 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ new Vditor('vditor', {
},
},
preview: {
hljs: {
style: 'github',
},
show: true,
url: '/api/markdown',
parse: () => {
LazyLoadImage()
},
Expand Down
1 change: 1 addition & 0 deletions demo/demo.scss
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
background-color: rgba(0, 0, 0, 0.04);
background-size: 20px 20px;
border-radius: 5px;
display: block;
}

kbd {
Expand Down
42 changes: 37 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"src/*"
],
"dependencies": {
"highlight.js": "^9.15.6",
"markdown-it": "^8.4.2",
"turndown": "^5.0.3",
"turndown-plugin-gfm": "^1.0.2"
},
Expand Down
30 changes: 28 additions & 2 deletions src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Preview {
}
}

public render(vditor: IVditor, value?: string) {
public async render(vditor: IVditor, value?: string) {
if (this.element.style.display === "none") {
return;
}
Expand Down Expand Up @@ -55,7 +55,33 @@ export class Preview {
}));
}, vditor.options.preview.delay);
} else {
this.element.innerHTML = vditor.editor.element.value;
const {default: MarkdownIt} = await import(/* webpackChunkName: "markdown-it" */ "markdown-it");
const hljsOpt: IHljsOptions = {
html: true,
linkify: true,
typographer: true,
}
if (vditor.options.preview.hljs.style) {
if (!document.getElementById('vditorHljsStyle')){
const hljsStyle = document.createElement("link")
hljsStyle.id = 'vditorHljsStyle'
hljsStyle.setAttribute("rel", "stylesheet")
hljsStyle.setAttribute("type", "text/css")
hljsStyle.setAttribute("href", `https://cdn.jsdelivr.net/npm/[email protected]/styles/${vditor.options.preview.hljs.style}.min.css`)
document.getElementsByTagName('head')[0].appendChild(hljsStyle)
}
}
if (vditor.options.preview.hljs.enable) {
const {default: hljs} = await import(/* webpackChunkName: "highlight.js" */ "highlight.js");
hljsOpt.highlight = (str: string, lang: string) => {
if (lang && hljs.getLanguage(lang)) {
return hljs.highlight(lang, str, true).value
}
return hljs.highlightAuto(str).value;
}
}
const md = new MarkdownIt(hljsOpt);
this.element.innerHTML = md.render(vditor.editor.element.value);
}
}
}
15 changes: 15 additions & 0 deletions src/ts/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ declare module "*.svg" {

declare module "*.png";

declare module "highlight.js";

declare module "markdown-it";

declare module "turndown";

declare module "turndown-plugin-gfm/lib/turndown-plugin-gfm.es.js";
Expand All @@ -14,6 +18,13 @@ declare var webkitAudioContext: {
new(contextOptions?: AudioContextOptions): AudioContext,
};

declare interface IHljsOptions {
html: boolean
linkify: boolean
typographer: boolean
highlight?(str: string, lang: string): string
}

declare interface ITurndown {
prototype: {
escape(name: string): string,
Expand Down Expand Up @@ -75,6 +86,10 @@ interface IPreview {
show?: boolean;
parse?: (element: HTMLElement) => void;
url?: string;
hljs: {
style: string,
enable: boolean
}
}

interface IHintData {
Expand Down
7 changes: 7 additions & 0 deletions src/ts/util/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export class Options {
lang: "zh_CN",
placeholder: "",
preview: {
hljs: {
style: 'atom-one-light',
enable: true
},
delay: 1000,
show: false,
},
Expand Down Expand Up @@ -185,6 +189,9 @@ export class Options {
}

if (this.options.preview) {
if (this.options.preview.hljs) {
this.options.preview.hljs = Object.assign({}, this.defaultOptions.preview.hljs, this.options.preview.hljs);
}
this.options.preview = Object.assign({}, this.defaultOptions.preview, this.options.preview);
}

Expand Down

0 comments on commit 138d10e

Please sign in to comment.