Skip to content
Merged
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 @@ -113,6 +113,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,19 @@
import { Parent as HASTParent } from "hast";
import { visit } from "unist-util-visit";

// Originally, rehypeParse parses videos as links, which is incorrect.
export function convertVideoToMarkdown() {
return (tree: HASTParent) => {
visit(tree, "element", (node, index, parent) => {
if (parent && 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 @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@nperez0111 how do you feel about checking if a file is a video based on a hard coded list of file endings? The alternative would be to get it from the meta.fileBlockAccept field in the video block's implementation. It would be nice to use this instead of a hard coded list, but I'm wary of what happens if someone replaces the default video block with their own. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think hard-coding is fine for now, this is what the markdown parser would've done anyway.

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;
}
}
3 changes: 3 additions & 0 deletions 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,5 @@
<img
src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
alt="BlockNote image"
data-url="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,4 @@
<video
src="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
data-url="https://interactive-examples.mdn.mozilla.net/media/cc0-videos/flower.webm"
></video>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![BlockNote image](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,22 @@
[
{
"attrs": {
"id": "1",
},
"content": [
{
"attrs": {
"backgroundColor": "default",
"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,22 @@
[
{
"attrs": {
"id": "1",
},
"content": [
{
"attrs": {
"backgroundColor": "default",
"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,
},
{
testCase: {
name: "inlineContent/mentionWithToExternalHTML",
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 @@ -1078,4 +1078,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,
},
];
8 changes: 1 addition & 7 deletions tests/src/unit/core/testSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import {
createInlineContentSpec,
createPageBreakBlockSpec,
createStyleSpec,
defaultBlockSpecs,
defaultInlineContentSpecs,
defaultProps,
defaultStyleSpecs,
} from "@blocknote/core";

// BLOCKS ----------------------------------------------------------------------
Expand Down Expand Up @@ -196,21 +193,18 @@ const FontSize = createStyleSpec(

// SCHEMA ----------------------------------------------------------------------

export const testSchema = BlockNoteSchema.create({
export const testSchema = BlockNoteSchema.create().extend({
blockSpecs: {
...defaultBlockSpecs,
pageBreak: createPageBreakBlockSpec(),
customParagraph: CustomParagraph,
simpleCustomParagraph: SimpleCustomParagraph,
simpleImage: SimpleImage,
},
inlineContentSpecs: {
...defaultInlineContentSpecs,
mention: Mention,
tag: Tag,
},
styleSpecs: {
...defaultStyleSpecs,
small: Small,
fontSize: FontSize,
},
Expand Down
Loading
Loading