hexo-link-obsidian修改


原本的插件有些问题,但是我也不记得是哪些了,当时改完就没有记录,只记得使用这个修改就行。ヾ(≧▽≦*)

使用端口为 5225

index.js 修改为

const { convertLinks } = require("./converter");
const path = require("path");
const fs = require("fs").promises;
const log = require('hexo-log')({
  debug: false,
  silent: false
});

let post_render = 0;
let links = [];

hexo.extend.filter.register(
  "before_post_render",
  async function (data) {
    post_render = 1;
    const config = this.config.link_obsidian;
    let content = data.content;
    try {
      content = data.content = await convertLinks(data, config && config.port || undefined, this.config.permalink.split(':abbrlink')[0]);
    } catch (err) {
      log.info('hexo-link-obsidian failed to convert', data.source, ': ', err.message);
    }
    data.content = content;
    return data;
  },
);

hexo.extend.filter.register(
  "before_exit",
  async function () {
    post_render && log.info(`hexo-link-obsidian Convert && Copy ${links.length} wikiLink files success!`);
  },
  1
);

/* -------------------- 文件目录生成 -------------------- */

/**
 * 路径是否存在,不存在则创建
 * @param {string} dir 路径
 */
async function dirExists(dir) {
  try {
    const stats = await fs.stat(dir);
    if (stats.isDirectory()) {
      return true;
    } else {
      return false;
    }
  } catch (err) {
    // 路径不存在
    const parentDir = path.dirname(dir);
    const parentExists = await dirExists(parentDir);
    if (parentExists) {
      await fs.mkdir(dir);
      return true;
    } else {
      return false;
    }
  }
}

converter.js修改为

"use strict";  
  
/*! *****************************************************************************  
Copyright (c) Microsoft Corporation.  
  
Permission to use, copy, modify, and/or distribute this software for any  
purpose with or without fee is hereby granted.  
  
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH  
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY  
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM  
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR  
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  
PERFORMANCE OF THIS SOFTWARE.  
***************************************************************************** */  
function t(t, e, n, i) {  
    return new (n || (n = Promise))((function (l, o) {  
        function r(t) {  
            try {  
                a(i.next(t))  
            } catch (t) {  
                o(t)  
            }  
        }  
  
        function c(t) {  
            try {  
                a(i.throw(t))  
            } catch (t) {  
                o(t)  
            }  
        }  
  
        function a(t) {  
            var e;  
            t.done ? l(t.value) : (e = t.value, e instanceof n ? e : new n((function (t) {  
                t(e)  
            }))).then(r, c)  
        }  
  
        a((i = i.apply(t, e || [])).next())  
    }))  
}  
  
Object.defineProperty(exports, "__esModule", {value: !0});  
const {readlinkSync: e, lstatSync: n} = require("fs"), i = require("path"), l = require("axios");  
let o = 5225;  
  
function r(e, n) {  
    return t(this, void 0, void 0, (function* () {  
        return (yield l.get(`http://127.0.0.1:${o}`, {params: {fileLink: e, sourcePath: n}, timeout: 1e3})).data  
    }))  
}  
  
let c = "posts/";  
const a = t => {  
    let e = t, n = e.match(/^abbrlink\:\s*(\w+)/g);  
    return n && (e = e.replace(n[0], "")), e  
}, s = (e, n) => t(void 0, void 0, void 0, (function* () {  
    let t = e, i = R(e), l = i.filter((t => "wiki" === t.type));  
    for (let e of l) {  
        let i = yield u("markdown", e.linkText, e.altOrBlockRef, n);  
        t = t.replace(e.match, i)  
    }  
    let o = i.filter((t => "wikiTransclusion" === t.type));  
    for (let e of o) {  
        let i = yield u("mdTransclusion", e.linkText, e.altOrBlockRef, n);  
        t = t.replace(e.match, i)  
    }  
    return t  
})), u = (l, o, a, s) => t(void 0, void 0, void 0, (function* () {  
    let t = o, u = "", f = "", d = decodeURI(t),  
        h = n(s.full_source).isSymbolicLink() ? e(s.full_source) : s.full_source, p = yield r(d, h);  
    t = "md" === p.extension ? function (t) {  
        let e, n = t.content, i = /^abbrlink\:\s*([\'\"])*(\w+)\1+?/m, l = n.match(i);  
        return e = l ? c + l[2] : "404", e  
    }(p) : function (t) {  
        let e;  
        return e = i.join(t.vault.adapter.basePath, "/", t.path), e  
    }(p);  
    let m = a.search(/\|/), k = a, v = a;  
    return -1 != m && (v = a.split("|")[0], k = a.split("|")[1]), u = "" !== k ? k : p ? p.basename : t, "mdTransclusion" === l && (f = v, a.startsWith("^") ? (f = encodeURI(f.slice(1)), f = `^${f}`) : f = encodeURI(f)), ["md"].includes(p.extension) ? `<a href="/${t}${f && "#" + f}.html" data-pjax-state target="_Blank">${u}</a>` : ["png", "jpg", "jpeg", "gif"].includes(p.extension) ? `![${u}](${encodeURI(t)})` : ["mp4", "webm", "ogg"].includes(p.extension) ? `<video src="![](${encodeURI(t)})" ${decodeURI(f)}></video>` : `[${u}](${encodeURI(t)})`  
}));  
const f = /\[\[(.*?)#.*?\]\]/, d = /(?<=\[\[)(.*)(?=#)/, h = /(?<=#).*?(?=]])/, p = /\[.*?]\((.*?)#.*?\)/,  
    m = /(?<=\]\()(.*)(?=#)/, k = /(?<=#).*?(?=\))/, v = t => p.test(t), y = t => {  
        let e = f.test(t), n = p.test(t);  
        if (e || n) {  
            let n = t.match(e ? d : m);  
            if (n) return n[0]  
        }  
        return ""  
    }, x = t => {  
        let e = f.test(t), n = p.test(t);  
        if (e || n) {  
            let n = t.match(e ? h : k);  
            if (n) return n[0]  
        }  
        return ""  
    }, R = t => {  
        const e = [];  
        let n = t, i = n.match(/\!*\[\[.*?\]\]/g);  
        if (i) {  
            let t = /(?<=\[\[).*?(?=(\]|\|))/, n = /(?<=\|).*(?=]])/;  
            for (let o of i) {  
                if (l = o, f.test(l)) {  
                    let t = y(o), n = x(o);  
                    if ("" !== t && "" !== n) {  
                        let i = {type: "wikiTransclusion", match: o, linkText: t, altOrBlockRef: n};  
                        e.push(i);  
                        continue                    }  
                }  
                let i = o.match(t);  
                if (i) {  
                    if (i[0].startsWith("http")) continue;  
                    let t = o.match(n), l = {type: "wiki", match: o, linkText: i[0], altOrBlockRef: t ? t[0] : ""};  
                    e.push(l)  
                }  
            }  
        }  
        var l;  
        let o = n.match(/\[(^$|.*?)\]\((.*?)\)/g);  
        if (o) {  
            let t = /(?<=\().*(?=\))/, n = /(?<=\[)(^$|.*?)(?=\])/;  
            for (let i of o) {  
                if (v(i)) {  
                    let t = y(i), n = x(i);  
                    if ("" !== t && "" !== n) {  
                        let l = {type: "mdTransclusion", match: i, linkText: t, altOrBlockRef: n};  
                        e.push(l);  
                        continue                    }  
                }  
                let l = i.match(t);  
                if (l) {  
                    if (l[0].startsWith("http")) continue;  
                    let t = i.match(n), o = {type: "markdown", match: i, linkText: l[0], altOrBlockRef: t ? t[0] : ""};  
                    e.push(o)  
                }  
            }  
        }  
        return e  
    };  
exports.convertLinks = (e, n = o, i = c) => t(void 0, void 0, void 0, (function* () {  
    o = n, c = i;  
    let t = e.content, l = a(t);  
    return l = yield s(t, e), l  
})), exports.getFirstLinkpathDest = r;  


如果本文帮助到了你,帮我点个广告可以咩(o′┏▽┓`o)


文章作者: Anubis
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Anubis !
评论
  目录