fix: clean up ofm code for video parsing

This commit is contained in:
Jacky Zhao 2024-01-17 09:45:05 -08:00
parent f6299da182
commit d7d5d8253c

View File

@ -1,10 +1,10 @@
import { QuartzTransformerPlugin } from "../types" import { QuartzTransformerPlugin } from "../types"
import { Root, Html, BlockContent, DefinitionContent, Paragraph, Code } from "mdast" import { Root, Html, Image, BlockContent, DefinitionContent, Paragraph, Code } from "mdast"
import { Element, Literal, Root as HtmlRoot } from "hast" import { Element, Literal, Root as HtmlRoot } from "hast"
import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace" import { ReplaceFunction, findAndReplace as mdastFindReplace } from "mdast-util-find-and-replace"
import { slug as slugAnchor } from "github-slugger" import { slug as slugAnchor } from "github-slugger"
import rehypeRaw from "rehype-raw" import rehypeRaw from "rehype-raw"
import { visit } from "unist-util-visit" import { SKIP, visit } from "unist-util-visit"
import path from "path" import path from "path"
import { JSResource } from "../../util/resources" import { JSResource } from "../../util/resources"
// @ts-ignore // @ts-ignore
@ -39,7 +39,7 @@ const defaultOptions: Options = {
parseBlockReferences: true, parseBlockReferences: true,
enableInHtmlEmbed: false, enableInHtmlEmbed: false,
enableYouTubeEmbed: true, enableYouTubeEmbed: true,
enableVideoEmbed: false, enableVideoEmbed: true,
} }
const icons = { const icons = {
@ -357,17 +357,15 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
plugins.push(() => { plugins.push(() => {
return (tree: Root, _file) => { return (tree: Root, _file) => {
visit(tree, "image", (node, index, parent) => { visit(tree, "image", (node, index, parent) => {
const match = node.url.match(videoExtensionRegex) if (parent && index != undefined && videoExtensionRegex.test(node.url)) {
if (parent && match) { console.log("replacin")
const htmlNode: PhrasingContent = { const newNode: Html = {
type: "html", type: "html",
value: `<video controls src="${node.url}" controls></video>`, value: `<video controls src="${node.url}"></video>`,
}
if (index && index >= 0 && index < parent.children.length) {
parent.children.splice(index, 1, htmlNode)
} else {
console.warn("Warning: Invalid index, htmlNode not added")
} }
parent.children.splice(index, 1, newNode)
return SKIP
} }
}) })
} }