feat: Native video display (#5866)

This commit is contained in:
Tom Moor
2023-09-28 20:28:09 -04:00
committed by GitHub
parent bd06e03b1e
commit f4fd9dae5f
24 changed files with 840 additions and 344 deletions

View File

@@ -28,13 +28,14 @@ function isAttachment(token: Token) {
// internal
// external (public share are pre-signed and this is a reasonable way of detecting them)
href?.startsWith("/api/attachments.redirect") ||
href?.startsWith("/api/files.get") ||
((href?.startsWith(env.AWS_S3_UPLOAD_BUCKET_URL) ||
href?.startsWith(env.AWS_S3_ACCELERATE_URL)) &&
href?.includes("X-Amz-Signature"))
);
}
export default function linksToAttachments(md: MarkdownIt) {
export default function linksToNodes(md: MarkdownIt) {
md.core.ruler.after("breaks", "attachments", (state) => {
const tokens = state.tokens;
let insideLink;
@@ -64,20 +65,29 @@ export default function linksToAttachments(md: MarkdownIt) {
// converted to a file attachment
if (insideLink && isAttachment(insideLink)) {
const { content } = current;
// convert to attachment token
const token = new Token("attachment", "a", 0);
token.attrSet("href", insideLink.attrGet("href") || "");
const parts = content.split(" ");
const size = parts.pop();
const title = parts.join(" ");
token.attrSet("size", size || "0");
token.attrSet("title", title);
if (size?.includes("x")) {
// convert to video
const token = new Token("video", "video", 0);
token.attrSet("src", insideLink.attrGet("href") || "");
token.attrSet("width", size.split("x")[0] || "0");
token.attrSet("height", size.split("x")[1] || "0");
token.attrSet("title", title);
tokens.splice(i - 1, 3, token);
} else {
// convert to attachment token
const token = new Token("attachment", "a", 0);
token.attrSet("href", insideLink.attrGet("href") || "");
token.attrSet("size", size || "0");
token.attrSet("title", title);
tokens.splice(i - 1, 3, token);
}
// delete the inline link this makes the assumption that the
// attachment is the only thing in the para.
tokens.splice(i - 1, 3, token);
insideLink = null;
break;
}