Compare commits

..

4 commits

3 changed files with 47 additions and 13 deletions

View file

@ -1,7 +1,7 @@
{
"id": "one-step-wiki-link",
"name": "One Step Wiki Link",
"version": "1.0.1",
"version": "1.0.4",
"minAppVersion": "1.8.0",
"description": "一步添加 wiki 链接",
"author": "Busyo",

View file

@ -1,6 +1,6 @@
{
"name": "one-step-wiki-link",
"version": "1.0.1",
"version": "1.0.4",
"description": "一步添加 wiki 链接",
"main": "main.js",
"scripts": {

View file

@ -1,6 +1,7 @@
import { Editor, MarkdownView, normalizePath, Plugin, TFile } from "obsidian";
import { Editor, EditorPosition, MarkdownView, normalizePath, Plugin, TFile } from "obsidian";
import { OneStepWikiLinkPluginSettingTab } from "./setting";
import { Localization } from "./localization";
import { start } from "repl";
interface OneStepWikiLinkPluginSettings {
@ -102,6 +103,8 @@ export default class OneStepWikiLinkPlugin extends Plugin {
this.openEditor = leaf.view.app.workspace.activeEditor?.editor;
// console.log(this.openEditor);
if (this.openEditor) {
this.currentFileName = ((leaf.view as MarkdownView).file as TFile).basename;
// console.log((leaf.view as MarkdownView).file?.basename);
this.checkContent(this.openEditor.getValue());
}
} else if (type == "empty") {
@ -135,6 +138,18 @@ export default class OneStepWikiLinkPlugin extends Plugin {
}
});
this.addCommand({
id: "convert-all-matching-words-to-wiki-links-zh",
name: "转换所有匹配的文本为维基链接",
// hotkeys: [{
// modifiers: ['Mod'],
// key: 'r'
// }],
editorCallback: (editor) => {
this.convert2WikiLink();
}
});
//监听文本变化
this.registerEvent(this.app.workspace.on("editor-change", async (file, data) => {
@ -147,7 +162,8 @@ export default class OneStepWikiLinkPlugin extends Plugin {
this.registerEvent(this.app.vault.on("rename", (file, oldPath) => {
if (file && file instanceof TFile) {
this.updateFileNameList(file.basename, true, oldPath.replace(".md", ""));
let fileName = oldPath.substring(oldPath.lastIndexOf('/') + 1).replace(".md", "");
this.updateFileNameList(file.basename, true, fileName);
}
}));
@ -189,6 +205,8 @@ export default class OneStepWikiLinkPlugin extends Plugin {
this.fileNameList.push(file.basename);
}
this.fileNameList.sort((a, b) => a.length > b.length ? -1 : 1);
}
updateFileNameList(name: string, add: boolean, extra: string = "") {
@ -213,18 +231,19 @@ export default class OneStepWikiLinkPlugin extends Plugin {
this.divForDetails.empty();
}
const contentWithoutLinks = data.replace(/\[\[([^\[\]]+)\]\]/g, "");
let contentWithoutLinks = data.replace(/\[\[([^\[\]]+)\]\]/g, "");
this.fileNameList.forEach(fileName => {
let regex = new RegExp(`(?<!\\[\\[)${fileName}\\b(?!\\]\\])`, "g");
let regex = new RegExp(`${fileName}\\b`, "g");
//检测字符串最后一个字符是否为有边界的语言
if (this.isNonBoundaryChar(fileName.charAt(fileName.length - 1))) {
regex = new RegExp(`(?<!\\[\\[)${fileName}(?!\\]\\])`, "g");
regex = new RegExp(`${fileName}`, "g");
}
const newContent = contentWithoutLinks.replace(regex, "");
// 排除当前文件
if (fileName !== this.currentFileName && regex.exec(contentWithoutLinks) !== null) {
if (fileName !== this.currentFileName && newContent !== contentWithoutLinks) {
this.matchingFiles.push(fileName);
//添加详情
@ -234,6 +253,8 @@ export default class OneStepWikiLinkPlugin extends Plugin {
text: fileName
});
}
contentWithoutLinks = newContent;
}
});
@ -261,6 +282,7 @@ export default class OneStepWikiLinkPlugin extends Plugin {
if (this.openEditor) {
let data = this.openEditor.getValue();
let rawChanges: { start: number; end: number; change: { from: EditorPosition, to: EditorPosition, text: string } }[] = [];
let changes = [];
for (const match of this.matchingFiles) {
@ -276,13 +298,25 @@ export default class OneStepWikiLinkPlugin extends Plugin {
let pos = this.openEditor.offsetToPos(res.index);
let endPos = { ch: pos.ch + match.length, line: pos.line };
changes.push({
from: pos,
to: endPos,
text: `[[${match}]]`
rawChanges.push({
start: res.index,
end: res.index + match.length,
change: {
from: pos,
to: endPos,
text: `[[${match}]]`
}
});
}
}
// data = data.replace(regex, `[[${match}]]`);
rawChanges.sort((a, b) => a.start - b.start);
let endIndex = -1;
for (let raw of rawChanges) {
if (raw.start >= endIndex) {
changes.push(raw.change);
endIndex = raw.end;
}
}