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 @@ -110,6 +110,7 @@
"remark-rehype": "^11.1.2",
"remark-stringify": "^11.0.0",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"uuid": "^8.3.2",
"y-prosemirror": "^1.3.7",
"y-protocols": "^1.0.6",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/api/exporters/markdown/markdownExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import {
StyleSchema,
} from "../../../schema/index.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)
export function cleanHTMLToMarkdown(cleanHTMLString: string) {
const markdownString = unified()
.use(rehypeParse, { fragment: true })
.use(convertVideoToMarkdown)
.use(removeUnderlines)
.use(addSpacesToCheckboxes)
.use(rehypeRemark)
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})`,
};
Comment on lines +7 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the test cases, I don't know that this is working entirely correctly.

It is being rendered to a link here: tests/src/unit/core/formatConversion/export/snapshots/html/video.html which I don't fully get why. So, it makes sense why this parse would not actually pick it up.

This may have to do with the way we output internal html vs external HTML?

I think we should hold off on merging this until we change up the default block API in #1904

}
});
};
}
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 @@ -14,6 +14,7 @@ import {
StyleSchema,
} from "../../../schema/index.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 @@ -54,13 +55,43 @@ 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 function markdownToHTML(markdown: string): string {
const htmlString = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype, {
handlers: {
...(remarkRehypeDefaultHandlers as any),
image: (state: any, node: any) => {
const url = String(node?.url || "");

if (isVideoUrl(url)) {
return video(state, node);
} else {
return remarkRehypeDefaultHandlers.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;
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div
class="bn-block-content"
data-content-type="image"
data-url="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
data-file-block=""
>
<div class="bn-file-block-content-wrapper" style="position: relative;">
<div class="bn-visual-media-wrapper">
<img
class="bn-visual-media"
src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
alt="BlockNote image"
draggable="false"
/>
</div>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div
class="bn-block-content"
data-content-type="video"
data-url="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
data-file-block=""
>
<div class="bn-file-block-content-wrapper" style="position: relative;">
<div class="bn-visual-media-wrapper">
<video
class="bn-visual-media"
src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
controls=""
draggable="false"
width="0"
></video>
</div>
</div>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<a
href="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
data-url="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
>https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<a
href="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
data-url="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
>https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"attrs": {
"backgroundColor": "default",
"id": "1",
"textColor": "default",
},
"content": [
{
"attrs": {
"caption": "",
"name": "",
"previewWidth": undefined,
"showPreview": true,
"textAlignment": "left",
"url": "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
},
"type": "image",
},
],
"type": "blockContainer",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"attrs": {
"backgroundColor": "default",
"id": "1",
"textColor": "default",
},
"content": [
{
"attrs": {
"caption": "",
"name": "",
"previewWidth": undefined,
"showPreview": true,
"textAlignment": "left",
"url": "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm",
},
"type": "video",
},
],
"type": "blockContainer",
},
]
28 changes: 28 additions & 0 deletions tests/src/unit/core/formatConversion/export/exportTestInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,34 @@ export const exportTestInstancesBlockNoteHTML: TestInstance<
},
executeTest: testExportBlockNoteHTML,
},
{
testCase: {
name: "image",
content: [
{
type: "image",
props: {
url: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
},
},
],
},
executeTest: testExportBlockNoteHTML,
},
{
testCase: {
name: "video",
content: [
{
type: "video",
props: {
url: "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm",
},
},
],
},
executeTest: testExportBlockNoteHTML,
},
];

export const exportTestInstancesHTML: TestInstance<
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"children": [],
"content": undefined,
"id": "1",
"props": {
"backgroundColor": "default",
"caption": "",
"name": "Image",
"showPreview": true,
"textAlignment": "left",
"url": "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
},
"type": "image",
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"children": [],
"content": undefined,
"id": "1",
"props": {
"backgroundColor": "default",
"caption": "",
"name": "",
"showPreview": true,
"textAlignment": "left",
"url": "https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm",
},
"type": "video",
},
]
14 changes: 14 additions & 0 deletions tests/src/unit/core/formatConversion/parse/parseTestInstances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,4 +996,18 @@ Regular paragraph`,
},
executeTest: testParseMarkdown,
},
{
testCase: {
name: "image",
content: `![Image](https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png)`,
},
executeTest: testParseMarkdown,
},
{
testCase: {
name: "video",
content: `![Video](https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm)`,
},
executeTest: testParseMarkdown,
},
];
Loading