-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from seyf1elislam/dev
v2
- Loading branch information
Showing
11 changed files
with
281 additions
and
89 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,84 @@ | ||
import { Editor, Notice } from "obsidian"; | ||
import { DEFAULT_SETTINGS } from "settings"; | ||
import { MediaType } from "types"; | ||
//deprecated function to embed media | ||
export function embedMediOld( | ||
input: Editor | string, | ||
settings: typeof DEFAULT_SETTINGS = DEFAULT_SETTINGS, | ||
embedType: MediaType | ||
): string | void { | ||
try { | ||
let filePath: string; | ||
|
||
if (typeof input === "string") { | ||
filePath = input; | ||
} else { | ||
filePath = input.getSelection(); | ||
} | ||
|
||
const port: number = settings.port || DEFAULT_SETTINGS.port; | ||
const baselink: string = settings.baselink || DEFAULT_SETTINGS.baselink; | ||
|
||
if (!filePath) { | ||
new Notice("File path not provided"); | ||
return; | ||
} | ||
|
||
filePath = decodeURIComponent(filePath); | ||
if (filePath.startsWith("file:///")) { | ||
filePath = filePath.replace("file:///", ""); | ||
} | ||
// Check if the file path is a valid file or link (starts with C:\ or / or https:// or http://) | ||
const isWindowsPath = filePath.match(/^[A-Za-z]:(\\|\/)/); | ||
const isUnixPath = filePath.match(/^\//); | ||
const isLink = filePath.match(/^https?:\/\//); | ||
|
||
let url: string; | ||
|
||
if (isLink) { | ||
// If it's a link, embed it directly without adding anything | ||
url = filePath; | ||
} else if (isWindowsPath || isUnixPath) { | ||
// If it's a file path, prepend the local server address | ||
const encodedPath = encodeURIComponent(filePath); | ||
url = `${baselink}:${port}/?q=${encodedPath}`; | ||
} else { | ||
new Notice("The provided file path or link is not valid."); | ||
return; | ||
} | ||
|
||
let embedCode: string; | ||
|
||
if (embedType === "auto") { | ||
if (filePath.match(/\.(mp4|webm|ogg)$/)) { | ||
embedType = "video"; | ||
} else if (filePath.match(/\.(mp3|wav|ogg)$/)) { | ||
embedType = "audio"; | ||
} else { | ||
embedType = "iframe"; | ||
} | ||
} | ||
|
||
if (embedType === "video") { | ||
embedCode = `<video width="640" height="360" controls> | ||
<source src="${url}" type="video/mp4"> | ||
Your browser does not support the video tag. | ||
</video>`; | ||
} else if (embedType === "audio") { | ||
embedCode = `<audio controls> | ||
<source src="${url}" type="audio/mpeg"> | ||
Your browser does not support the audio tag. | ||
</audio>`; | ||
} else { | ||
embedCode = `<iframe src="${url}" width="640" height="360" frameborder="0" allowfullscreen></iframe>`; | ||
} | ||
|
||
if (typeof input === "string") { | ||
return embedCode; | ||
} else { | ||
input.replaceSelection(embedCode); | ||
} | ||
} catch (error) { | ||
console.log("Error :", error); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,97 +1,130 @@ | ||
import { Notice, Editor, Menu, MenuItem } from "obsidian"; | ||
import { DEFAULT_SETTINGS } from "settings"; | ||
import { MediaBlockType, MediaType } from "types"; | ||
|
||
export type EmbedType = "video" | "iframe" | "audio" | "image" | "auto"; | ||
export function embedMediaAsCodeBlock(editor: Editor): void { | ||
try { | ||
let filePath = editor.getSelection(); | ||
if (!filePath) { | ||
new Notice("File path not provided"); | ||
return; | ||
} | ||
|
||
filePath = filePath.replace("file:///", ""); | ||
|
||
let embedType = determineEmbedType(filePath); | ||
|
||
let codeBlock = `\`\`\`media | ||
path: ${filePath} | ||
type: ${embedType} | ||
`; | ||
if (embedType === "video" || embedType === "iframe") { | ||
codeBlock += `width: ${640} | ||
height: ${360} | ||
`; | ||
} | ||
|
||
codeBlock += `\`\`\``; | ||
|
||
editor.replaceSelection(codeBlock); | ||
} catch (error) { | ||
console.log("Error:", error); | ||
} | ||
} | ||
|
||
export function embedMedia( | ||
export function onEditorMenu( | ||
menu: Menu, | ||
editor: Editor, | ||
settings: typeof DEFAULT_SETTINGS = DEFAULT_SETTINGS, | ||
embedType: EmbedType | ||
showInMenuItem: boolean = true | ||
) { | ||
if (!showInMenuItem) return; | ||
try { | ||
const filePath = editor.getSelection(); | ||
menu.addItem((item: MenuItem) => { | ||
item.setTitle("Embed selected media path") | ||
.setIcon("link") | ||
.onClick(async () => { | ||
if (!editor) return; | ||
//TODO replace tihs default with the actual settings | ||
// embedMediOld(editor, DEFAULT_SETTINGS, "auto"); | ||
embedMediaAsCodeBlock(editor); | ||
}); | ||
}); | ||
} catch (error) { | ||
console.log("Error :", error); | ||
} | ||
return; | ||
} | ||
export function generateMediaView( | ||
mediainfo: MediaBlockType, | ||
settings: typeof DEFAULT_SETTINGS = DEFAULT_SETTINGS | ||
): string { | ||
try { | ||
let filePath: string = mediainfo.path; | ||
|
||
const port: number = settings.port || DEFAULT_SETTINGS.port; | ||
const baselink: string = settings.baselink || DEFAULT_SETTINGS.baselink; | ||
|
||
if (!filePath) { | ||
new Notice("File path not provided"); | ||
return; | ||
return ""; | ||
} | ||
|
||
// Check if the file path is a valid file or link (starts with C:\ or / or https://) | ||
const isWindowsPath = filePath.match(/^[A-Za-z]:(\\|\/)/); | ||
const isUnixPath = filePath.match(/^\//); | ||
const isLink = filePath.match(/^https?:\/\//); | ||
if (filePath.startsWith("file:///")) | ||
filePath = filePath.replace("file:///", ""); | ||
filePath = decodeURIComponent(filePath); | ||
|
||
let url: string; | ||
if (!isValidPath(filePath)) { | ||
new Notice("The provided file path or link is not valid."); | ||
return ""; | ||
} | ||
|
||
if (isLink) { | ||
// If it's a link, embed it directly without adding anything | ||
let url: string; | ||
if (filePath.match(/^https?:\/\//)) { | ||
url = filePath; | ||
} else if (isWindowsPath || isUnixPath) { | ||
// If it's a file path, prepend the local server address | ||
} else { | ||
const encodedPath = encodeURIComponent(filePath); | ||
url = `${baselink}:${port}/?q=${encodedPath}`; | ||
} else { | ||
new Notice("The provided file path or link is not valid."); | ||
return; | ||
} | ||
|
||
let embedCode: string; | ||
|
||
if (embedType === "auto") { | ||
if (filePath.match(/\.mp4$/)) { | ||
embedType = "video"; | ||
} else if (filePath.match(/\.mp3$/)) { | ||
embedType = "audio"; | ||
} else if (filePath.match(/\.png$|\.jpg$|\.jpeg$/)) { | ||
embedType = "image"; | ||
} else { | ||
embedType = "iframe"; | ||
} | ||
} | ||
let embedType: MediaType = | ||
mediainfo.type || determineEmbedType(filePath); | ||
|
||
const width = mediainfo.width ?? 640; | ||
const height = mediainfo.height ?? 360; | ||
|
||
if (embedType === "video") { | ||
embedCode = `<video width="640" height="360" controls> | ||
return `<video width="${width}" height="${height}" controls> | ||
<source src="${url}" type="video/mp4"> | ||
Your browser does not support the video tag. | ||
</video>`; | ||
} else if (embedType === "audio") { | ||
embedCode = `<audio controls> | ||
<source src="${url}" type="audio/mpeg"> | ||
Your browser does not support the audio tag. | ||
return `<audio controls> | ||
<source src="${url}" type="audio/mpeg"> | ||
Your browser does not support the audio tag. | ||
</audio>`; | ||
} else if (embedType === "image") { | ||
// embedCode = `<img src="${url}" alt="image" width="640" height="360">`; | ||
embedCode = `![](${url})`; | ||
} else { | ||
embedCode = `<iframe src="${url}" width="640" height="360" frameborder="0" allowfullscreen></iframe>`; | ||
return `<iframe src="${url}" width="${width}" height="${height}" frameborder="0" allowfullscreen></iframe>`; | ||
} | ||
|
||
editor.replaceSelection(embedCode); | ||
} catch (error) { | ||
console.log("Error :", error); | ||
console.log("Error:", error); | ||
return ""; | ||
} | ||
} | ||
|
||
export function onEditorMenu( | ||
menu: Menu, | ||
editor: Editor, | ||
showInMenuItem: boolean = true | ||
) { | ||
if (!showInMenuItem) return; | ||
try { | ||
menu.addItem((item: MenuItem) => { | ||
item.setTitle("Embed selected link [LocalMediaEmbed]") | ||
.setIcon("link") | ||
.onClick(async () => { | ||
if (!editor) return; | ||
embedMedia(editor, DEFAULT_SETTINGS, "auto"); | ||
}); | ||
}); | ||
} catch (error) { | ||
console.log("Error :", error); | ||
function isValidPath(filePath: string): boolean { | ||
const isWindowsPath = filePath.match(/^[A-Za-z]:(\\|\/)/) !== null; | ||
const isUnixPath = filePath.match(/^\//) !== null; | ||
const isLink = filePath.match(/^https?:\/\//) !== null; | ||
return isWindowsPath || isUnixPath || isLink; | ||
} | ||
|
||
export function determineEmbedType(filePath: string): MediaType { | ||
filePath = filePath.replace("file:///", ""); | ||
if (filePath.match(/\.(mp4|webm|ogg)$/)) { | ||
return "video"; | ||
} else if (filePath.match(/\.(mp3|wav|ogg)$/)) { | ||
return "audio"; | ||
} else { | ||
return "iframe"; | ||
} | ||
return; | ||
} |
Oops, something went wrong.