Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"remark-rehype": "^11.1.1",
"remark-stringify": "^11.0.0",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"uuid": "^8.3.2",
"y-prosemirror": "^1.3.4",
"y-protocols": "^1.0.6",
Expand Down
8 changes: 6 additions & 2 deletions packages/core/src/api/exporters/markdown/markdownExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
initializeESMDependencies,
} from "../../../util/esmDependencies.js";
import { createExternalHTMLExporter } from "../html/externalHTMLExporter.js";
import { removeUnderlines } from "./removeUnderlinesRehypePlugin.js";
import { removeUnderlines } from "./util/removeUnderlinesRehypePlugin.js";
import { addSpacesToCheckboxes } from "./util/addSpacesToCheckboxesRehypePlugin.js";
import { convertVideoToMarkdown } from "./util/convertVideoToMarkdownRehypePlugin.js";

// Needs to be sync because it's used in drag handler event (SideMenuPlugin)
// Ideally, call `await initializeESMDependencies()` before calling this function
Expand All @@ -28,12 +29,15 @@ export function cleanHTMLToMarkdown(cleanHTMLString: string) {
const markdownString = deps.unified
.unified()
.use(deps.rehypeParse.default, { fragment: true })
.use(convertVideoToMarkdown)
.use(removeUnderlines)
.use(addSpacesToCheckboxes)
.use(deps.rehypeRemark.default)
.use(deps.remarkGfm.default)
.use(deps.remarkStringify.default, {
handlers: { text: (node) => node.value },
handlers: {
text: (node) => node.value,
},
})
.processSync(cleanHTMLString);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { visit } from "unist-util-visit";

// Originally, rehypeParse parses videos as links, which is incorrect.
export function convertVideoToMarkdown() {
return (tree: any) => {
visit(tree, "element", (node, index, parent) => {
if (node.tagName === "video") {
const src = node.properties?.src || node.properties?.["data-url"] || "";
const name =
node.properties?.title || node.properties?.["data-name"] || "";
parent.children[index!] = {
type: "text",
value: `![${name}](${src})`,
};
}
});
};
}
31 changes: 31 additions & 0 deletions packages/core/src/api/parsers/markdown/parseMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "../../../schema/index.js";
import { initializeESMDependencies } from "../../../util/esmDependencies.js";
import { HTMLToBlocks } from "../html/parseHTML.js";
import { isVideoUrl } from "../../../util/string.js";

// modified version of https://github.com/syntax-tree/mdast-util-to-hast/blob/main/lib/handlers/code.js
// that outputs a data-language attribute instead of a CSS class (e.g.: language-typescript)
Expand Down Expand Up @@ -48,6 +49,27 @@ function code(state: any, node: any) {
return result;
}

function video(state: any, node: any) {
const url = String(node?.url || "");
const title = node?.title ? String(node.title) : undefined;

let result: any = {
type: "element",
tagName: "video",
properties: {
src: url,
"data-name": title,
"data-url": url,
controls: true,
},
children: [],
};
state.patch?.(node, result);
result = state.applyData ? state.applyData(node, result) : result;

return result;
}

export async function markdownToHTML(markdown: string): Promise<string> {
const deps = await initializeESMDependencies();

Expand All @@ -58,6 +80,15 @@ export async function markdownToHTML(markdown: string): Promise<string> {
.use(deps.remarkRehype.default, {
handlers: {
...(deps.remarkRehype.defaultHandlers as any),
image: (state: any, node: any) => {
const url = String(node?.url || "");

if (isVideoUrl(url)) {
return video(state, node);
} else {
return deps.remarkRehype.defaultHandlers.image(state, node);
}
},
code,
},
})
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ export function filenameFromURL(url: string): string {
}
return parts[parts.length - 1];
}

export function isVideoUrl(url: string) {
const videoExtensions = [
"mp4",
"webm",
"ogg",
"mov",
"mkv",
"flv",
"avi",
"wmv",
"m4v",
];
try {
const pathname = new URL(url).pathname;
const ext = pathname.split(".").pop()?.toLowerCase() || "";
return videoExtensions.includes(ext);
} catch (_) {
return false;
}
}
Loading