@lvce-editor/api 9.37.0 → 9.37.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { activate } from './parts/Activation/Activation.ts';
2
2
  export { getColorThemeNames } from './parts/ColorTheme/ColorTheme.ts';
3
+ export { markdownToVirtualDom } from './parts/Markdown/Markdown.ts';
3
4
  export { getAccessToken, type GetAccessTokenOptions } from './parts/Authentication/Authentication.ts';
4
5
  export { createElectronWebContentsView } from './parts/ElectronWebContentsView/ElectronWebContentsView.ts';
5
6
  export { executeCommand } from './parts/ExecuteCommand/ExecuteCommand.ts';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { activate } from "./parts/Activation/Activation.js";
2
2
  export { getColorThemeNames } from "./parts/ColorTheme/ColorTheme.js";
3
+ export { markdownToVirtualDom } from "./parts/Markdown/Markdown.js";
3
4
  export { getAccessToken } from "./parts/Authentication/Authentication.js";
4
5
  export { createElectronWebContentsView } from "./parts/ElectronWebContentsView/ElectronWebContentsView.js";
5
6
  export { executeCommand } from "./parts/ExecuteCommand/ExecuteCommand.js";
@@ -0,0 +1,2 @@
1
+ import type { VirtualDomNode } from '@lvce-editor/virtual-dom-worker';
2
+ export declare const markdownToVirtualDom: (markdown: string) => Promise<readonly VirtualDomNode[]>;
@@ -0,0 +1,7 @@
1
+ import { executeCommand } from "../ExecuteCommand/ExecuteCommand.js";
2
+ import { sanitizeMarkdownVirtualDom } from "./SanitizeMarkdownVirtualDom.js";
3
+ export const markdownToVirtualDom = async (markdown) => {
4
+ const html = (await executeCommand('Markdown.renderMarkdown', markdown));
5
+ const nodes = (await executeCommand('Markdown.getVirtualDom', html));
6
+ return sanitizeMarkdownVirtualDom(nodes);
7
+ };
@@ -0,0 +1,2 @@
1
+ import type { VirtualDomNode } from '@lvce-editor/virtual-dom-worker';
2
+ export declare const sanitizeMarkdownVirtualDom: (nodes: readonly VirtualDomNode[]) => readonly VirtualDomNode[];
@@ -0,0 +1,30 @@
1
+ import { VirtualDomElements } from '@lvce-editor/virtual-dom-worker';
2
+ const allowedHrefProtocols = new Set(['http', 'https', 'mailto', 'tel']);
3
+ const allowedSrcProtocols = new Set(['http', 'https']);
4
+ const protocolPattern = /^([a-z][a-z\d+.-]*):/i;
5
+ const isAllowedUrl = (value, allowedProtocols) => {
6
+ const normalizedValue = [...value]
7
+ .filter((character) => {
8
+ const codePoint = character.codePointAt(0) ?? 0;
9
+ return codePoint > 32 && codePoint !== 127;
10
+ })
11
+ .join('');
12
+ const protocol = protocolPattern.exec(normalizedValue)?.[1]?.toLowerCase();
13
+ return !protocol || allowedProtocols.has(protocol);
14
+ };
15
+ export const sanitizeMarkdownVirtualDom = (nodes) => {
16
+ return nodes.map((node) => {
17
+ const sanitizedNode = { ...node };
18
+ if (typeof sanitizedNode.href === 'string' && !isAllowedUrl(sanitizedNode.href, allowedHrefProtocols)) {
19
+ delete sanitizedNode.href;
20
+ }
21
+ if (sanitizedNode.type === VirtualDomElements.A && typeof sanitizedNode.href === 'string') {
22
+ sanitizedNode.target = '_blank';
23
+ sanitizedNode.rel = 'noopener noreferrer';
24
+ }
25
+ if (typeof sanitizedNode.src === 'string' && !isAllowedUrl(sanitizedNode.src, allowedSrcProtocols)) {
26
+ delete sanitizedNode.src;
27
+ }
28
+ return sanitizedNode;
29
+ });
30
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/api",
3
- "version": "9.37.0",
3
+ "version": "9.37.2",
4
4
  "description": "Tree-shakeable extension API for Lvce Editor extensions.",
5
5
  "keywords": [
6
6
  "lvce-editor",