Skip to content

Commit

Permalink
feat: support inner md link: [[abc]] -> [](/abc) -> https://www.blog.…
Browse files Browse the repository at this point in the history
  • Loading branch information
kirito41dd committed Apr 20, 2024
1 parent fc0f386 commit 6ed798e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "hugo-publish",
"name": "Hugo Publish",
"version": "1.0.4",
"version": "1.0.5",
"minAppVersion": "0.15.0",
"description": "Publish your blog to hugo site.",
"author": "kirito",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "1.0.4",
"version": "1.0.5",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down
40 changes: 26 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export default class HugoPublishPlugin extends Plugin {

const meta = this.app.metadataCache.getFileCache(f);

const link2path: Map<string, string> = new Map();
// link -> path,is_md
const link2path: Map<string, [string, boolean]> = new Map();

const abf = this.app.vault.getAbstractFileByPath(f.path);
// copy files to blog dir
Expand All @@ -185,7 +186,7 @@ export default class HugoPublishPlugin extends Plugin {
for (const v of meta.embeds) {
const embed_f = this.app.metadataCache.getFirstLinkpathDest(v.link, f.path);
if (embed_f) {
link2path.set(v.link, embed_f.path);
link2path.set(v.link, [embed_f.path, false]);
const src = path.join(this.base_path, embed_f.path);
const dst = path.join(this.settings.get_static_abs_dir(), embed_f.path);
//console.log(`copy ${src} to ${dst}`);
Expand All @@ -194,16 +195,19 @@ export default class HugoPublishPlugin extends Plugin {
}

}
// if (meta?.links) {
// for (const v of meta.links) {
// const link_f = this.app.metadataCache.getFirstLinkpathDest(v.link, f.path);
// console.log("link", v.link, link_f);
// if (link_f) {
// link2path.set(v.link, link_f.path);
// const src =
// }
// }
// }
if (meta?.links) {
for (const v of meta.links) {
const link_f = this.app.metadataCache.getFirstLinkpathDest(v.link, f.path);
//console.log("link", v.link, link_f);
if (link_f) {
let is_md = false;
if (link_f.path.endsWith(".md")) {
is_md = true;
link2path.set(v.link, [v.link, is_md]);
}
}
}
}
const static_dir = this.settings.static_dir;

//console.log("this", this);
Expand All @@ -212,14 +216,22 @@ export default class HugoPublishPlugin extends Plugin {
const decoded_url = decodeURI(node.url);
const v = link2path.get(decoded_url)
if (v) {
node.url = encodeURI(path.join("/", static_dir, v).replace(/\\/g, '/'));
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [vv, _is_md] = v;
node.url = encodeURI(path.join("/", static_dir, vv).replace(/\\/g, '/'));
}
})
visit(ast, 'link', function (node, index, parent) {
const decoded_url = decodeURI(node.url);
const v = link2path.get(decoded_url)
if (v) {
node.url = encodeURI(path.join("/", static_dir, v).replace(/\\/g, '/'));
const [vv, is_md] = v;
if (is_md) {
// inner md link: [[abc]] -> [](/abc) -> https://www.blog.com/abc
node.url = encodeURI(path.join("/", vv).replace(/\\/g, '/'));
} else {
node.url = encodeURI(path.join("/", static_dir, vv).replace(/\\/g, '/'));
}
}
})

Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"1.0.0": "0.15.0",
"1.0.2": "0.15.0",
"1.0.3": "0.15.0",
"1.0.4": "0.15.0"
"1.0.4": "0.15.0",
"1.0.5": "0.15.0"
}

0 comments on commit 6ed798e

Please sign in to comment.