Skip to content

Commit

Permalink
完成了把id修改为block链接的block编码形式
Browse files Browse the repository at this point in the history
  • Loading branch information
norcx committed Apr 27, 2024
1 parent 83ffd4e commit fdbe254
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
在原来代码的基础上,添加了两个功能
1. 让obsidian markdown文件中的卡片在anki的deck的结构与文件夹结构保存一致
例如:"/root/hello/world.md"中的卡片会自动同步到"root::hello::world"中
2. 添加了从anki直接跳转到obsidian markdown文件对应的block的链接
2. 把id修改为block链接的形式,从而实现anki直接跳转到obsidian markdown文件对应的block
![图 1](images/5913712e835c128fdc7a12c0c0c1caa006ac7d47bf85a3a9f6c5970e37a0a948.png)

# 本项目使用方法
将main.js和manifes.json文件替换obsidian to anki中的main.js manifes.json
Expand All @@ -18,7 +19,8 @@
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
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".

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.
2.Change the id to a block link so that anki jumps directly to the corresponding block in the obsidian markdown file.
![图 2](images/1cfcb1bb28a06e354b339691fe79a5d40c32ac3d8d74a0c1a5fd1b4f554e201a.png)

# How to use
Replace the main.js and manifes.json file with the main.js and manifes.json in Obsidian to Anki
Expand Down
7 changes: 4 additions & 3 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"settings": {
"CUSTOM_REGEXPS": {
"Basic": "((?:[^\\n][\\n]?)+) #flashcard ?\\n*((?:\\n(?:^.{1,3}$|^.{4}(?<!<!--).*))+)",
"Basic": "",
"Basic+": "",
"Basic++": "",
"Basic+++": "",
Expand All @@ -16,7 +16,7 @@
"图片遮盖": "",
"填空题": "",
"红宝书": "",
"问答题": "",
"问答题": "((?:[^\\n][\\n]?)+)\\n\\?{2} *\\n((?:\\n?(?:^.{1,3}$|^.{4}(?<!<!--)(?<!\\^ID-).*))+)",
"问答题(同时生成翻转的卡片<可选>)": "",
"问答题(同时生成翻转的卡片)": "",
"问答题(输入答案)": ""
Expand Down Expand Up @@ -117,7 +117,8 @@
"norcx'anki'test/未命名.md": "68a672a3c453707a5db64bd4da5324c5",
"未命名.md": "d41d8cd98f00b204e9800998ecf8427e",
"norcx'anki'test/挖空测试.md": "08d3f20ea7d3869cf4e63b8ad32533ae",
"未命名 1 1.md": "9d5ac601c16c5b988e5351736714eaa9"
"未命名 1 1.md": "166ac5eb16bb207e951f3c68aa1f4943",
"一次添加成功测试.md": "988168da191b9968488324f0f79360fb"
},
"fields_dict": {
"Basic": [
Expand Down
42 changes: 25 additions & 17 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,6 @@ export class AllFile extends AbstractFile {
}
}

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

scanFile() {
this.setupScan()
Expand All @@ -453,9 +439,6 @@ export class AllFile extends AbstractFile {
for (let note_type in this.custom_regexps) {
const regexp_str: string = this.custom_regexps[note_type]
if (regexp_str) {
if(this.add_card_link){
this.scanLink2ob(regexp_str)
}
this.search(note_type, regexp_str)
}
}
Expand All @@ -466,6 +449,31 @@ export class AllFile extends AbstractFile {
fix_newline_ids() {
this.file = this.file.replace(double_regexp, "$1")
}
getAddNotesWithId(): AnkiConnect.AnkiConnectRequest {
let actions: AnkiConnect.AnkiConnectRequest[] = [];
this.all_notes_to_add.forEach((note, index) => {
// 使用this.note_ids数组中的相应ID
let id = this.note_ids[index];
if (id !== null) {
let updated = false; // 标志是否成功替换
// 遍历note.fields,查找"ID-null"的子字符串,替换为"ID-"+String(id)
for (let key in note.fields) {
let originalValue = note.fields[key];
note.fields[key] = originalValue.replace(/ID-null/g, "ID-" + id);
// 如果字段被更新,则设置 updated 为 true
if (originalValue !== note.fields[key]) {
updated = true;
}
}
// 如果成功替换,则添加更新操作,并继续到下一个笔记
if (updated) {
actions.push(AnkiConnect.updateNoteFields(id, note.fields));
}
}
});
return AnkiConnect.multi(actions);
}


writeIDs() {
let normal_inserts: [number, string][] = []
Expand Down
5 changes: 5 additions & 0 deletions src/files-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AllFile } from './file'
import * as AnkiConnect from './anki'
import { basename } from 'path'
import multimatch from "multimatch"
import { AnkiConnectNote } from './interfaces/note-interface'
interface addNoteResponse {
result: number,
error: string | null
Expand Down Expand Up @@ -283,17 +284,21 @@ export class FileManager {
}
file.card_ids = temp
}
let temp: AnkiConnect.AnkiConnectRequest[] = []
for (let index in this.ownFiles) {
let i: number = parseInt(index)
let ownFile = this.ownFiles[i]
let obFile = this.files[i]
ownFile.tags = tag_list
ownFile.writeIDs()
ownFile.removeEmpties()
temp.push(ownFile.getAddNotesWithId())
if (ownFile.file !== ownFile.original_file) {
await this.app.vault.modify(obFile, ownFile.file)
}
}
console.info("Requesting addition of id links...")
await AnkiConnect.invoke('multi', {actions: temp})
await this.requests_2()
}

Expand Down
2 changes: 1 addition & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class FormatConverter {
}

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

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

0 comments on commit fdbe254

Please sign in to comment.