Skip to content

Commit

Permalink
enableVideoEmbed plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbailin committed Jan 14, 2024
1 parent 1a8aedf commit d460e87
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions quartz/plugins/transformers/ofm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { PhrasingContent } from "mdast-util-find-and-replace/lib"
import { capitalize } from "../../util/lang"
import { PluggableList } from "unified"


export interface Options {
comments: boolean
highlight: boolean
Expand All @@ -26,6 +27,7 @@ export interface Options {
parseBlockReferences: boolean
enableInHtmlEmbed: boolean
enableYouTubeEmbed: boolean
enableVideoEmbed: boolean
}

const defaultOptions: Options = {
Expand All @@ -37,7 +39,8 @@ const defaultOptions: Options = {
parseTags: true,
parseBlockReferences: true,
enableInHtmlEmbed: false,
enableYouTubeEmbed: false,
enableYouTubeEmbed: true,
enableVideoEmbed: false,
}

const icons = {
Expand Down Expand Up @@ -130,6 +133,8 @@ const calloutLineRegex = new RegExp(/^> *\[\!\w+\][+-]?.*$/, "gm")
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)\b/, "g")


export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options> | undefined> = (
userOpts,
Expand All @@ -149,19 +154,17 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
if (src instanceof Buffer) {
src = src.toString()
}

src = src.replaceAll(calloutLineRegex, (value) => {
// force newline after title of callout
return value + "\n> "
})
}

// pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex)
if (opts.wikilinks) {
// pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex)
if (opts.wikilinks) {
if (src instanceof Buffer) {
src = src.toString()
}

src = src.replaceAll(wikilinkRegex, (value, ...capture) => {
const [rawFp, rawHeader, rawAlias]: (string | undefined)[] = capture

Expand Down Expand Up @@ -265,6 +268,8 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
},
])
}



if (opts.highlight) {
replacements.push([
Expand Down Expand Up @@ -346,11 +351,33 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
}
})
}

mdastFindReplace(tree, replacements)
}
})

//go through mdast, find img nodes with video extension, convert them to html nodes
if (opts.enableVideoEmbed) {
plugins.push(() => {
return (tree: Root, _file) => {
visit(tree, "image", (node, index, parent) => {
const match = node.url.match(videoExtensionRegex)
if (match && parent) {
const htmlNode: PhrasingContent = {
type: "html",
value: `<video controls autoplay src="${node.url}" controls></video>`
}
if (typeof index === 'number') {
parent.children.splice(index, 1, htmlNode);
} else {
console.log("Error: index is not a number")
}
}
})
}
})
}


if (opts.callouts) {
plugins.push(() => {
return (tree: Root, _file) => {
Expand Down Expand Up @@ -507,7 +534,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
}
})
}

if (opts.enableYouTubeEmbed) {
plugins.push(() => {
return (tree: HtmlRoot) => {
Expand Down Expand Up @@ -578,3 +605,4 @@ declare module "vfile" {
htmlAst: HtmlRoot
}
}

0 comments on commit d460e87

Please sign in to comment.