-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
162 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,7 @@ package-lock.json | |
|
||
# build | ||
main.js | ||
*.js.map | ||
*.js.map | ||
|
||
# saved data | ||
data.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { MarkdownPostProcessor, MarkdownPostProcessorContext } from "obsidian"; | ||
|
||
export function processInternalEmbeds(el:HTMLElement, ctx:MarkdownPostProcessorContext) { | ||
|
||
const internalEmbedObs = new MutationObserver( | ||
// Update embed's src to include temporal fragment when it is loaded | ||
(mutationsList, observer) => { | ||
// See also: https://www.w3.org/TR/media-frags/#valid-uri | ||
const tFragRegex = /(?<=#)t=([\w:\.]*?)(?:,([\w:\.]+?))?$/; | ||
for (const m of mutationsList) { | ||
if (m.addedNodes.length) { | ||
switch (m.addedNodes[0].nodeName) { | ||
case "VIDEO": | ||
case "AUDIO": | ||
const matchArray = (m.target as HTMLSpanElement) | ||
.getAttr("src") | ||
.match(tFragRegex); | ||
if (matchArray) { | ||
const embed = m.addedNodes[0] as HTMLMediaElement; | ||
const tFrag = matchArray[0]; | ||
const url = new URL(embed.src); | ||
url.hash = tFrag; | ||
embed.src = url.toString(); | ||
} | ||
break; | ||
case "IMG": | ||
// Do nothing | ||
break; | ||
default: | ||
throw new TypeError( | ||
`Unexpected addnote type: ${m.addedNodes[0].nodeName}` | ||
); | ||
} | ||
observer.disconnect(); | ||
} | ||
} | ||
}); | ||
|
||
const obsConfig = { attributes: false, childList: true, subtree: false }; | ||
|
||
for (const span of el.querySelectorAll("span.internal-embed")) { | ||
internalEmbedObs.observe(span, obsConfig); | ||
} | ||
}; | ||
|
||
export function processExternalEmbeds(el:HTMLElement, ctx:MarkdownPostProcessorContext) { | ||
console.log(el.innerHTML); | ||
for (const e of el.querySelectorAll("img[referrerpolicy]")) { | ||
console.log(e.outerHTML); | ||
const srcEl = e as HTMLImageElement; | ||
const ext = new URL(srcEl.src).pathname.split(".").last(); | ||
|
||
let newEl: HTMLMediaElement; | ||
let type: "audio" | "video"; | ||
switch (ext) { | ||
case "mp3": case "wav": case "m4a": case "ogg": case "3gp": case "flac": | ||
type = "audio"; | ||
break; | ||
case "mp4": case "webm": case "ogv": | ||
type = "video"; | ||
break; | ||
} | ||
console.log(ext); | ||
if (type) { | ||
console.log('hello'); | ||
newEl = createEl(type); | ||
newEl.src = srcEl.src; | ||
newEl.controls = true; | ||
srcEl.parentNode.replaceChild(newEl, srcEl); | ||
} | ||
} | ||
} |