Skip to content

Commit fdbe254

Browse files
committed
完成了把id修改为block链接的block编码形式
1 parent 83ffd4e commit fdbe254

7 files changed

+39
-23
lines changed
99.5 KB
Loading
98.9 KB
Loading

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
在原来代码的基础上,添加了两个功能
44
1. 让obsidian markdown文件中的卡片在anki的deck的结构与文件夹结构保存一致
55
例如:"/root/hello/world.md"中的卡片会自动同步到"root::hello::world"中
6-
2. 添加了从anki直接跳转到obsidian markdown文件对应的block的链接
6+
2. 把id修改为block链接的形式,从而实现anki直接跳转到obsidian markdown文件对应的block
7+
![图 1](images/5913712e835c128fdc7a12c0c0c1caa006ac7d47bf85a3a9f6c5970e37a0a948.png)
78

89
# 本项目使用方法
910
将main.js和manifes.json文件替换obsidian to anki中的main.js manifes.json
@@ -18,7 +19,8 @@
1819
This project was forked by [Obsidian_to_Anki](https://github.com/Pseudonium/Obsidian_to_Anki) Based on the original code, two features have been added
1920
1. Let the cards in the Obsidian markdown file be synchronized with the folder structure in the anki. For example, cards in "/root/hello/world.md" will be automatically synchronized to "root::hello::world".
2021

21-
2. Added a link to jump from anki to the corresponding block of the obsidian markdown file. .js file to replace main.js in Obsidian to Anki.
22+
2.Change the id to a block link so that anki jumps directly to the corresponding block in the obsidian markdown file.
23+
![图 2](images/1cfcb1bb28a06e354b339691fe79a5d40c32ac3d8d74a0c1a5fd1b4f554e201a.png)
2224

2325
# How to use
2426
Replace the main.js and manifes.json file with the main.js and manifes.json in Obsidian to Anki

data.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"settings": {
33
"CUSTOM_REGEXPS": {
4-
"Basic": "((?:[^\\n][\\n]?)+) #flashcard ?\\n*((?:\\n(?:^.{1,3}$|^.{4}(?<!<!--).*))+)",
4+
"Basic": "",
55
"Basic+": "",
66
"Basic++": "",
77
"Basic+++": "",
@@ -16,7 +16,7 @@
1616
"图片遮盖": "",
1717
"填空题": "",
1818
"红宝书": "",
19-
"问答题": "",
19+
"问答题": "((?:[^\\n][\\n]?)+)\\n\\?{2} *\\n((?:\\n?(?:^.{1,3}$|^.{4}(?<!<!--)(?<!\\^ID-).*))+)",
2020
"问答题(同时生成翻转的卡片<可选>)": "",
2121
"问答题(同时生成翻转的卡片)": "",
2222
"问答题(输入答案)": ""
@@ -117,7 +117,8 @@
117117
"norcx'anki'test/未命名.md": "68a672a3c453707a5db64bd4da5324c5",
118118
"未命名.md": "d41d8cd98f00b204e9800998ecf8427e",
119119
"norcx'anki'test/挖空测试.md": "08d3f20ea7d3869cf4e63b8ad32533ae",
120-
"未命名 1 1.md": "9d5ac601c16c5b988e5351736714eaa9"
120+
"未命名 1 1.md": "166ac5eb16bb207e951f3c68aa1f4943",
121+
"一次添加成功测试.md": "988168da191b9968488324f0f79360fb"
121122
},
122123
"fields_dict": {
123124
"Basic": [

src/file.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -431,20 +431,6 @@ export class AllFile extends AbstractFile {
431431
}
432432
}
433433

434-
scanLink2ob(regexp_str: string) {
435-
let inserts = [];
436-
let regexp: RegExp = new RegExp(regexp_str, 'gm')
437-
for (let match of findignore(regexp, this.file, this.ignore_spans)) {
438-
if (match[0].includes("[🔗]")) {
439-
continue; // 如果包含 "[Card]",则跳过当前匹配
440-
}
441-
let id: string = Math.random().toString(36).substring(2, 8)
442-
let modified:string ="\n"+"[🔗]("+ this.formatter.getUrlFromLink( this.fullpath+"#^" + id )+") ^"+ id
443-
inserts.push([match.index + match[0].length, modified]);
444-
}
445-
this.file = string_insert(this.file, inserts);
446-
//this.file = this.file.replace(card_regexp, "$1")
447-
}
448434

449435
scanFile() {
450436
this.setupScan()
@@ -453,9 +439,6 @@ export class AllFile extends AbstractFile {
453439
for (let note_type in this.custom_regexps) {
454440
const regexp_str: string = this.custom_regexps[note_type]
455441
if (regexp_str) {
456-
if(this.add_card_link){
457-
this.scanLink2ob(regexp_str)
458-
}
459442
this.search(note_type, regexp_str)
460443
}
461444
}
@@ -466,6 +449,31 @@ export class AllFile extends AbstractFile {
466449
fix_newline_ids() {
467450
this.file = this.file.replace(double_regexp, "$1")
468451
}
452+
getAddNotesWithId(): AnkiConnect.AnkiConnectRequest {
453+
let actions: AnkiConnect.AnkiConnectRequest[] = [];
454+
this.all_notes_to_add.forEach((note, index) => {
455+
// 使用this.note_ids数组中的相应ID
456+
let id = this.note_ids[index];
457+
if (id !== null) {
458+
let updated = false; // 标志是否成功替换
459+
// 遍历note.fields,查找"ID-null"的子字符串,替换为"ID-"+String(id)
460+
for (let key in note.fields) {
461+
let originalValue = note.fields[key];
462+
note.fields[key] = originalValue.replace(/ID-null/g, "ID-" + id);
463+
// 如果字段被更新,则设置 updated 为 true
464+
if (originalValue !== note.fields[key]) {
465+
updated = true;
466+
}
467+
}
468+
// 如果成功替换,则添加更新操作,并继续到下一个笔记
469+
if (updated) {
470+
actions.push(AnkiConnect.updateNoteFields(id, note.fields));
471+
}
472+
}
473+
});
474+
return AnkiConnect.multi(actions);
475+
}
476+
469477

470478
writeIDs() {
471479
let normal_inserts: [number, string][] = []

src/files-manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AllFile } from './file'
55
import * as AnkiConnect from './anki'
66
import { basename } from 'path'
77
import multimatch from "multimatch"
8+
import { AnkiConnectNote } from './interfaces/note-interface'
89
interface addNoteResponse {
910
result: number,
1011
error: string | null
@@ -283,17 +284,21 @@ export class FileManager {
283284
}
284285
file.card_ids = temp
285286
}
287+
let temp: AnkiConnect.AnkiConnectRequest[] = []
286288
for (let index in this.ownFiles) {
287289
let i: number = parseInt(index)
288290
let ownFile = this.ownFiles[i]
289291
let obFile = this.files[i]
290292
ownFile.tags = tag_list
291293
ownFile.writeIDs()
292294
ownFile.removeEmpties()
295+
temp.push(ownFile.getAddNotesWithId())
293296
if (ownFile.file !== ownFile.original_file) {
294297
await this.app.vault.modify(obFile, ownFile.file)
295298
}
296299
}
300+
console.info("Requesting addition of id links...")
301+
await AnkiConnect.invoke('multi', {actions: temp})
297302
await this.requests_2()
298303
}
299304

src/format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class FormatConverter {
5858
}
5959

6060
format_note_with_url(note: AnkiConnectNote, url: string, field: string): void {
61-
note.fields[field] += '<br><a href="' + url + '" class="obsidian-link">Obsidian</a>'
61+
note.fields[field] += '<br><a href="' + url + '" class="obsidian-link">🔗</a>'
6262
}
6363

6464
format_note_with_frozen_fields(note: AnkiConnectNote, frozen_fields_dict: Record<string, Record<string, string>>): void {

0 commit comments

Comments
 (0)