From f6299da1823b51917792905ca71257e55f706ed6 Mon Sep 17 00:00:00 2001
From: Matthew Bailin <matthewdbailin@gmail.com>
Date: Wed, 17 Jan 2024 17:32:02 +0000
Subject: [PATCH] feat: add ofm option to transform `<img>` tags with video exts into `<video>` (closes #463) (#664)
---
quartz/plugins/transformers/ofm.ts | 34 ++++++++++++++++++++++++++++++----
1 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/quartz/plugins/transformers/ofm.ts b/quartz/plugins/transformers/ofm.ts
index 35257cc..8caad97 100644
--- a/quartz/plugins/transformers/ofm.ts
+++ b/quartz/plugins/transformers/ofm.ts
@@ -26,6 +26,7 @@
parseBlockReferences: boolean
enableInHtmlEmbed: boolean
enableYouTubeEmbed: boolean
+ enableVideoEmbed: boolean
}
const defaultOptions: Options = {
@@ -37,7 +38,8 @@
parseTags: true,
parseBlockReferences: true,
enableInHtmlEmbed: false,
- enableYouTubeEmbed: false,
+ enableYouTubeEmbed: true,
+ enableVideoEmbed: false,
}
const icons = {
@@ -130,6 +132,7 @@
const tagRegex = new RegExp(/(?:^| )#((?:[-_\p{L}\p{Emoji}\d])+(?:\/[-_\p{L}\p{Emoji}\d]+)*)/, "gu")
const blockReferenceRegex = new RegExp(/\^([A-Za-z0-9]+)$/, "g")
const ytLinkRegex = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/
+const videoExtensionRegex = new RegExp(/\.(mp4|webm|ogg|avi|mov|flv|wmv|mkv|mpg|mpeg|3gp|m4v)$/)
export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
userOpts,
@@ -346,11 +349,31 @@
}
})
}
-
mdastFindReplace(tree, replacements)
}
})
+ if (opts.enableVideoEmbed) {
+ plugins.push(() => {
+ return (tree: Root, _file) => {
+ visit(tree, "image", (node, index, parent) => {
+ const match = node.url.match(videoExtensionRegex)
+ if (parent && match) {
+ const htmlNode: PhrasingContent = {
+ type: "html",
+ value: `<video controls src="${node.url}" controls></video>`,
+ }
+ if (index && index >= 0 && index < parent.children.length) {
+ parent.children.splice(index, 1, htmlNode)
+ } else {
+ console.warn("Warning: Invalid index, htmlNode not added")
+ }
+ }
+ })
+ }
+ })
+ }
+
if (opts.callouts) {
plugins.push(() => {
return (tree: Root, _file) => {
@@ -366,7 +389,7 @@
}
const text = firstChild.children[0].value
- const restChildren = firstChild.children.slice(1)
+ const restOfTitle = firstChild.children.slice(1)
const [firstLine, ...remainingLines] = text.split("\n")
const remainingText = remainingLines.join("\n")
@@ -382,7 +405,10 @@
match.input.slice(calloutDirective.length).trim() || capitalize(calloutType)
const titleNode: Paragraph = {
type: "paragraph",
- children: [{ type: "text", value: titleContent + " " }, ...restChildren],
+ children:
+ restOfTitle.length === 0
+ ? [{ type: "text", value: titleContent + " " }]
+ : restOfTitle,
}
const title = mdastToHtml(titleNode)
--
Gitblit v1.10.0