generated from obsidianmd/obsidian-sample-plugin
-
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.
- Loading branch information
Showing
16 changed files
with
287 additions
and
63 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
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
Empty file.
Empty file.
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,51 @@ | ||
import { TAbstractFile } from "obsidian"; | ||
import { parse, Uri } from "path"; | ||
|
||
interface Attachment { | ||
name: string; | ||
path: string; | ||
source: string; | ||
} | ||
export class AttachmentMatch { | ||
|
||
|
||
getAttachments(markdown: string, filePath: TAbstractFile) { | ||
// 匹配两种语法 | ||
const regex = /!\[\[.*\]\]|!\((.*?)(\[\])/g; | ||
const attachments: Attachment[] = []; | ||
const matches = markdown.match(regex); | ||
if (matches) { | ||
matches.forEach((match) => { | ||
// 提取路径 | ||
let path = match.slice(3, -2); | ||
|
||
// 处理三种形式的路径 | ||
if (path.startsWith("../")) { | ||
// 相对路径 | ||
path = this.normalizePath(file.parent.path + "/" + path); | ||
} else if (!path.startsWith("/")) { | ||
// 短形式 | ||
path = this.normalizePath(file.parent.path + "/" + path); | ||
} else { | ||
// 绝对路径 | ||
path = this.normalizePath(path); | ||
} | ||
|
||
// 构造Attachment对象 | ||
const attachment = { | ||
name: path.split("/").pop(), | ||
path, | ||
source: match, | ||
}; | ||
|
||
attachments.push(attachment); | ||
}); | ||
} | ||
|
||
return attachments; | ||
} | ||
|
||
// 规范化路径,兼容不同系统 | ||
normalizePath(path: string) { | ||
return Uri.parse(path).toString(); | ||
}} |
Oops, something went wrong.