Skip to content

Commit

Permalink
#1158
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 committed Sep 25, 2024
1 parent db02d6a commit 5ed8c28
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/assets/less/_reset.less
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@
.language-mindmap,
.language-plantuml,
.language-mermaid,
.language-smiles,
.language-markmap,
.language-abc,
.language-flowchart,
Expand Down
71 changes: 71 additions & 0 deletions src/js/smiles-drawer/smiles-drawer.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {lazyLoadImageRender} from "./ts/markdown/lazyLoadImageRender";
import {mathRender} from "./ts/markdown/mathRender";
import {mediaRender} from "./ts/markdown/mediaRender";
import {mermaidRender} from "./ts/markdown/mermaidRender";
import {SMILESRender} from "./ts/markdown/SMILESRender";
import {markmapRender} from "./ts/markdown/markmapRender";
import {mindmapRender} from "./ts/markdown/mindmapRender";
import {outlineRender} from "./ts/markdown/outlineRender";
Expand All @@ -35,6 +36,8 @@ class Vditor {
public static mathRender = mathRender;
/** 流程图/时序图/甘特图渲染 */
public static mermaidRender = mermaidRender;
/** 化学物质结构渲染 */
public static SMILESRender = SMILESRender;
/** 支持markdown的思维导图 */
public static markmapRender = markmapRender;
/** flowchart.js 渲染 */
Expand Down
2 changes: 1 addition & 1 deletion src/ts/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class Constants {
"stackoverflow-light", "tokyo-night-light", "vs", "xcode", "default"];
public static readonly ALIAS_CODE_LANGUAGES: string[] = [
// 自定义
"abc", "plantuml", "mermaid", "flowchart", "echarts", "mindmap", "graphviz", "math", "markmap",
"abc", "plantuml", "mermaid", "flowchart", "echarts", "mindmap", "graphviz", "math", "markmap", "smiles",
// 别名
"js", "ts", "html", "toml", "c#", "bat"
];
Expand Down
3 changes: 2 additions & 1 deletion src/ts/export/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export const exportHTML = (vditor: IVditor) => {
math: ${JSON.stringify(vditor.options.preview.math)},
});
Vditor.mermaidRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
Vditor.markmapRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
Vditor.SMILESRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
Vditor.markmapRender(previewElement, '${vditor.options.cdn}');
Vditor.flowchartRender(previewElement, '${vditor.options.cdn}');
Vditor.graphvizRender(previewElement, '${vditor.options.cdn}');
Vditor.chartRender(previewElement, '${vditor.options.cdn}', '${vditor.options.theme}');
Expand Down
29 changes: 29 additions & 0 deletions src/ts/markdown/SMILESRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Constants} from "../constants";
import {addScript} from "../util/addScript";
import {SMILESRenderAdapter} from "./adapterRender";
import {genUUID} from "../util/function";

declare class SmiDrawer {
constructor(moleculeOptions: IObject, reactionOptions: IObject);

public draw: (code: string, id: string, theme?: string) => void;
}

export const SMILESRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN, theme: string) => {
const SMILESElements = SMILESRenderAdapter.getElements(element);
if (SMILESElements.length > 0) {
addScript(`${cdn}/dist/js/smiles-drawer/smiles-drawer.min.js?v=2.1.7`, "vditorAbcjsScript").then(() => {
let sd = new SmiDrawer({}, {});
SMILESElements.forEach((item: HTMLDivElement) => {
const code = SMILESRenderAdapter.getCode(item).trim();
if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
return;
}
const id = "smiles" + genUUID()
item.innerHTML = `<svg id="${id}"></svg>`;
sd.draw(code, '#' + id, theme === "dark" ? "dark" : undefined)
item.setAttribute("data-processed", "true");
});
});
}
};
12 changes: 8 additions & 4 deletions src/ts/markdown/adapterRender.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
export const mathRenderAdapter = {
getCode: (mathElement: Element) => mathElement.textContent,
getElements: (element: HTMLElement) => element.querySelectorAll(".language-math"),
getCode: (el: Element) => el.textContent,
getElements: (element: HTMLElement| Document) => element.querySelectorAll(".language-math"),
};
export const SMILESRenderAdapter = {
getCode: (el: Element) => el.textContent,
getElements: (element: HTMLElement| Document) => element.querySelectorAll(".language-smiles"),
};
export const mermaidRenderAdapter = {
/** 不仅要返回code,并且需要将 code 设置为 el 的 innerHTML */
getCode: (el: Element) => el.textContent,
getElements: (element: HTMLElement) => element.querySelectorAll(".language-mermaid"),
getElements: (element: HTMLElement| Document) => element.querySelectorAll(".language-mermaid"),
};
export const markmapRenderAdapter = {
getCode: (el: Element) => el.textContent,
getElements: (element: HTMLElement) => element.querySelectorAll(".language-markmap"),
getElements: (element: HTMLElement| Document) => element.querySelectorAll(".language-markmap"),
};
export const mindmapRenderAdapter = {
getCode: (el: Element) => el.getAttribute("data-code"),
Expand Down
2 changes: 1 addition & 1 deletion src/ts/markdown/codeRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const codeRender = (element: HTMLElement, option?: IHljs) => {
e.classList.contains("language-echarts") || e.classList.contains("language-mindmap") ||
e.classList.contains("language-plantuml") || e.classList.contains("language-markmap") ||
e.classList.contains("language-abc") || e.classList.contains("language-graphviz") ||
e.classList.contains("language-math")) {
e.classList.contains("language-math") || e.classList.contains("language-smiles")) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ts/markdown/highlightRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const highlightRender = (hljsOption?: IHljs, element: HTMLElement | Docum

if (block.classList.contains("language-mermaid") || block.classList.contains("language-flowchart") ||
block.classList.contains("language-echarts") || block.classList.contains("language-mindmap") ||
block.classList.contains("language-plantuml") ||
block.classList.contains("language-plantuml")|| block.classList.contains("language-smiles") ||
block.classList.contains("language-abc") || block.classList.contains("language-graphviz") ||
block.classList.contains("language-math")) {
return;
Expand Down
5 changes: 2 additions & 3 deletions src/ts/markdown/markmapRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ const init = (el: HTMLElement,code: string) => {
}


export const markmapRender = (element: HTMLElement, cdn = Constants.CDN, theme: string) => {
export const markmapRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN) => {
const markmapElements = markmapRenderAdapter.getElements(element);
if (markmapElements.length === 0) {
return;
}
addScript(`${cdn}/dist/js/markmap/markmap.min.js`, "vditorMermaidScript").then(() => {
addScript(`${cdn}/dist/js/markmap/markmap.min.js`, "vditorMarkerScript").then(() => {
markmapElements.forEach((item) => {
const code = markmapRenderAdapter.getCode(item);
if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
Expand All @@ -53,5 +53,4 @@ export const markmapRender = (element: HTMLElement, cdn = Constants.CDN, theme:
}
});
});

};
2 changes: 1 addition & 1 deletion src/ts/markdown/mathRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare global {
}
}

export const mathRender = (element: HTMLElement, options?: { cdn?: string, math?: IMath }) => {
export const mathRender = (element: (HTMLElement | Document) = document, options?: { cdn?: string, math?: IMath }) => {
const mathElements = mathRenderAdapter.getElements(element);

if (mathElements.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/ts/markdown/mermaidRender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare const mermaid: {
render(id: string, text: string): { svg: string }
};

export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme: string) => {
export const mermaidRender = (element: (HTMLElement | Document) = document, cdn = Constants.CDN, theme: string) => {
const mermaidElements = mermaidRenderAdapter.getElements(element);
if (mermaidElements.length === 0) {
return;
Expand Down Expand Up @@ -44,7 +44,7 @@ export const mermaidRender = (element: HTMLElement, cdn = Constants.CDN, theme:
if (item.getAttribute("data-processed") === "true" || code.trim() === "") {
return;
}
const id = "mermaid"+genUUID()
const id = "mermaid" + genUUID()
try {
const mermaidData = await mermaid.render(id, item.textContent);
item.innerHTML = mermaidData.svg;
Expand Down
8 changes: 5 additions & 3 deletions src/ts/markdown/previewRender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Constants} from "../constants";
import {setContentTheme} from "../ui/setContentTheme";
import {addScript, addScriptSync} from "../util/addScript";
import {addScript} from "../util/addScript";
import {hasClosestByClassName, hasClosestByMatchTag} from "../util/hasClosest";
import {merge} from "../util/merge";
import {abcRender} from "./abcRender";
Expand All @@ -14,7 +14,8 @@ import {lazyLoadImageRender} from "./lazyLoadImageRender";
import {mathRender} from "./mathRender";
import {mediaRender} from "./mediaRender";
import {mermaidRender} from "./mermaidRender";
import {markmapRender} from "../markdown/markmapRender";
import {markmapRender} from "./markmapRender";
import {SMILESRender} from "./SMILESRender";
import {mindmapRender} from "./mindmapRender";
import {plantumlRender} from "./plantumlRender";
import {setLute} from "./setLute";
Expand Down Expand Up @@ -131,7 +132,8 @@ export const previewRender = async (previewElement: HTMLDivElement, markdown: st
math: mergedOptions.math,
});
mermaidRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
markmapRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
SMILESRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
markmapRender(previewElement, mergedOptions.cdn);
flowchartRender(previewElement, mergedOptions.cdn);
graphvizRender(previewElement, mergedOptions.cdn);
chartRender(previewElement, mergedOptions.cdn, mergedOptions.mode);
Expand Down
4 changes: 3 additions & 1 deletion src/ts/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {highlightRender} from "../markdown/highlightRender";
import {mathRender} from "../markdown/mathRender";
import {mediaRender} from "../markdown/mediaRender";
import {mermaidRender} from "../markdown/mermaidRender";
import {SMILESRender} from "../markdown/SMILESRender";
import {markmapRender} from "../markdown/markmapRender";
import {mindmapRender} from "../markdown/mindmapRender";
import {plantumlRender} from "../markdown/plantumlRender";
Expand Down Expand Up @@ -224,7 +225,8 @@ export class Preview {
highlightRender(vditor.options.preview.hljs, vditor.preview.previewElement,
vditor.options.cdn);
mermaidRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
markmapRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
markmapRender(vditor.preview.previewElement, vditor.options.cdn);
SMILESRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
flowchartRender(vditor.preview.previewElement, vditor.options.cdn);
graphvizRender(vditor.preview.previewElement, vditor.options.cdn);
chartRender(vditor.preview.previewElement, vditor.options.cdn, vditor.options.theme);
Expand Down
5 changes: 4 additions & 1 deletion src/ts/util/processCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {mermaidRender} from "../markdown/mermaidRender";
import {markmapRender} from "../markdown/markmapRender";
import {mindmapRender} from "../markdown/mindmapRender";
import {plantumlRender} from "../markdown/plantumlRender";
import {SMILESRender} from "../markdown/SMILESRender";

export const processPasteCode = (html: string, text: string, type = "sv") => {
const tempElement = document.createElement("div");
Expand Down Expand Up @@ -67,8 +68,10 @@ export const processCodeRender = (previewPanel: HTMLElement, vditor: IVditor) =>
abcRender(previewPanel, vditor.options.cdn);
} else if (language === "mermaid") {
mermaidRender(previewPanel, vditor.options.cdn, vditor.options.theme);
} else if (language === "smiles") {
SMILESRender(previewPanel, vditor.options.cdn, vditor.options.theme);
} else if (language === "markmap") {
markmapRender(previewPanel, vditor.options.cdn, vditor.options.theme);
markmapRender(previewPanel, vditor.options.cdn);
} else if (language === "flowchart") {
flowchartRender(previewPanel, vditor.options.cdn);
} else if (language === "echarts") {
Expand Down

0 comments on commit 5ed8c28

Please sign in to comment.