Skip to content

Commit

Permalink
Added link to jump to obsidian block from anki
Browse files Browse the repository at this point in the history
  • Loading branch information
norcx committed Apr 19, 2024
1 parent 96bd30c commit 968c700
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 13 deletions.
154 changes: 148 additions & 6 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"settings": {
"CUSTOM_REGEXPS": {
"Basic": "",
"Basic": "((?:[^\\n][\\n]?)+) #flashcard ?\\n*((?:\\n(?:^.{1,3}$|^.{4}(?<!<!--).*))+)",
"Basic+": "",
"Basic++": "",
"Basic+++": "",
Expand Down Expand Up @@ -43,8 +43,16 @@
"问答题(输入答案)": "正面"
},
"CONTEXT_FIELDS": {},
"FOLDER_DECKS": {},
"FOLDER_TAGS": {},
"FOLDER_DECKS": {
"未命名": "",
"第一个文件夹": "",
"第一个文件夹/第二个文件夹": ""
},
"FOLDER_TAGS": {
"未命名": "",
"第一个文件夹": "",
"第一个文件夹/第二个文件夹": ""
},
"Syntax": {
"Begin Note": "START",
"End Note": "END",
Expand All @@ -66,13 +74,147 @@
"CurlyCloze - Highlights to Clozes": false,
"ID Comments": true,
"Add Obsidian Tags": false,
"Use Path as Deck": false
"Use Path as Deck": true
},
"IGNORED_FILE_GLOBS": [
"**/*.excalidraw.md"
]
},
"Added Media": [],
"File Hashes": {},
"fields_dict": {}
"File Hashes": {
"这个文件不会出现.md": "3bf0f4061ead725b9f00d10156e48c32",
"这次必须成功.md": "c8cb44b25bf0f5f3abdb489ce570dea7",
"第一个文件夹/读书.md": "d41d8cd98f00b204e9800998ecf8427e",
"第一个文件夹/读一本好书.md": "2bc012e3b6e73af53f1e2254e494d7d2",
"第一个文件夹/第二个文件夹/你好呀.md": "09639afc110da5e7033d4e6ce12147ff",
"第一个文件夹/第二个文件夹/未命名.md": "ef1e094f5b366fe563e71c1f6bcd141c",
"第一个文件夹/第二个文件夹/你好.md": "d41d8cd98f00b204e9800998ecf8427e",
"要成功了.md": "9ff4e52b269f2e12afbb019771ca51c7",
"破案吧.md": "03e25ab08d38fd6ea0c97b27ad4a4b4a",
"未命名 1.md": "4c811787fb4a175e57da923063c1e41c",
"有笔记但是没有内容.md": "284f5c9fb85e25bcd79b43b83b1c3e33",
"未命名 4.md": "295a6be3b2f9283e4d143f2dc27d7ff2",
"未命名 2.md": "30a44ea90cb104d9b16dda3826232784",
"成功.md": "bcab17d6d9aa24ec021d86219ba80275",
"你是谁.md": "7ce5d9b5499bf82ca585cd0556562db2",
"什么都没有.md": "2228e977ebea8966e27929f43e39cb67",
"README.md": "5844997ffd21e5020ecaa7670e5d942e"
},
"fields_dict": {
"Basic": [
"Front",
"Back"
],
"Basic+": [
"Front",
"Back"
],
"Basic++": [
"Front",
"Back"
],
"Basic+++": [
"Front",
"Back"
],
"Basic-73fde": [
"Front",
"Back"
],
"Cloze ALL": [
"文字",
"背面额外"
],
"Pot Card 2": [
"Front",
"Back",
"Symbol1",
"Voice1",
"Symbol2",
"Voice2"
],
"Python - Basic": [
"Front",
"Back - Basic",
"Back - Code",
"Additional Info",
"Example",
"Version"
],
"Python - Basic (and reversed card)": [
"Front",
"Back",
"Additional Info",
"With Options",
"Example",
"Version"
],
"Python - Cloze": [
"Text",
"Example",
"Version"
],
"UVBasic-English": [
"英语单词",
"英美音标",
"中文释义",
"vocab简明",
"vocab扩展",
"真题原句",
"柯林斯星级",
"柯林斯解释",
"来源",
"英语发音"
],
"单词": [
"单词",
"音标",
"助记词",
"线索",
"词性1",
"释义1",
"词性2",
"释义2",
"发音",
"拓展"
],
"图片遮盖": [
"遮盖",
"图片",
"标题",
"背面额外",
"注释"
],
"填空题": [
"文字",
"背面额外"
],
"红宝书": [
"单词",
"音标",
"解释",
"红宝书",
"字源",
"不择手段背单词",
"趣味全助记",
"Audio"
],
"问答题": [
"正面",
"背面"
],
"问答题(同时生成翻转的卡片<可选>)": [
"正面",
"背面",
"添加翻转的卡片"
],
"问答题(同时生成翻转的卡片)": [
"正面",
"背面"
],
"问答题(输入答案)": [
"正面",
"背面"
]
}
}
1 change: 1 addition & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class MyPlugin extends Plugin {
"ID Comments": true,
"Add Obsidian Tags": false,
"Use Path as Deck" : false,
"Add Card link": false
},
IGNORED_FILE_GLOBS: DEFAULT_IGNORED_FILE_GLOBS,
}
Expand Down
27 changes: 24 additions & 3 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FormatConverter } from './format'
import { CachedMetadata, HeadingCache } from 'obsidian'

const double_regexp: RegExp = /(?:\r\n|\r|\n)((?:\r\n|\r|\n)(?:<!--)?ID: \d+)/g
const card_regexp: RegExp = /(?:\r\n|\r|\n)((?:\r\n|\r|\n)[Card])/g

function id_to_str(identifier:number, inline:boolean = false, comment:boolean = false): string {
let result = "ID: " + identifier.toString()
Expand Down Expand Up @@ -93,7 +94,8 @@ abstract class AbstractFile {
formatter: FormatConverter
fullpath: string
use_path_as_deck:boolean
constructor(file_contents: string, path:string, url: string, data: FileData, file_cache: CachedMetadata, fullpath: string,use_path_as_deck:boolean) {
add_card_link:boolean
constructor(file_contents: string, path:string, url: string, data: FileData, file_cache: CachedMetadata, fullpath: string,use_path_as_deck:boolean, add_card_link:boolean) {
this.data = data
this.file = file_contents
this.path = path
Expand All @@ -103,6 +105,7 @@ abstract class AbstractFile {
this.formatter = new FormatConverter(file_cache, this.data.vault_name)
this.fullpath = fullpath
this.use_path_as_deck = use_path_as_deck
this.add_card_link = add_card_link
}

setup_frozen_fields_dict() {
Expand Down Expand Up @@ -267,8 +270,8 @@ export class AllFile extends AbstractFile {
regex_notes_to_add: AnkiConnectNote[]
regex_id_indexes: number[]

constructor(file_contents: string, path:string, url: string, data: FileData, file_cache: CachedMetadata,fullpath:string,use_path_as_deck:boolean) {
super(file_contents, path, url, data, file_cache, fullpath,use_path_as_deck)
constructor(file_contents: string, path:string, url: string, data: FileData, file_cache: CachedMetadata,fullpath:string,use_path_as_deck:boolean,add_card_link:boolean) {
super(file_contents, path, url, data, file_cache, fullpath,use_path_as_deck,add_card_link)
this.custom_regexps = data.custom_regexps
}

Expand Down Expand Up @@ -425,13 +428,31 @@ 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("[Card]")) {
continue; // 如果包含 "[Card]",则跳过当前匹配
}
let id: string = Math.random().toString(36).substring(2, 8)
let modified:string ="\n"+"[Card]("+ 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()
this.scanNotes()
this.scanInlineNotes()
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 Down
5 changes: 3 additions & 2 deletions src/files-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class FileManager {
async genAllFiles() {
for (let file of this.files) {
const content: string = await this.app.vault.read(file)
const fullpath: string = (file.path)
const fullpath: string = (file.path.slice(0, -file.extension.length - 1))
const cache: CachedMetadata = this.app.metadataCache.getCache(file.path)
const file_data = this.dataToFileData(file)
this.ownFiles.push(
Expand All @@ -147,7 +147,8 @@ export class FileManager {
file_data,
cache,
fullpath,
this.data.use_path_as_deck
this.data.use_path_as_deck,
this.data.add_card_link
)
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/settings-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export interface PluginSettings {
"CurlyCloze - Highlights to Clozes": boolean,
"ID Comments": boolean,
"Add Obsidian Tags": boolean,
"Use Path as Deck" : boolean
"Use Path as Deck" : boolean,
"Add Card link": boolean

},
IGNORED_FILE_GLOBS:string[]
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface FileData {
add_context: boolean
add_obs_tags: boolean
use_path_as_deck: boolean
add_card_link: boolean
}

export interface ParsedSettings extends FileData {
Expand Down
1 change: 1 addition & 0 deletions src/setting-to-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function settingToData(app: App, settings: PluginSettings, fields_d
result.add_context = settings.Defaults["Add Context"]
result.add_obs_tags = settings.Defaults["Add Obsidian Tags"]
result.use_path_as_deck= settings.Defaults["Use Path as Deck"]
result.add_card_link = settings.Defaults["Add Card link"]
result.ignored_file_globs = settings.IGNORED_FILE_GLOBS ?? [];

return result
Expand Down
6 changes: 5 additions & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const defaultDescs = {
"CurlyCloze - Highlights to Clozes": "Convert ==highlights== -> {highlights} to be processed by CurlyCloze.",
"ID Comments": "Wrap note IDs in a HTML comment.",
"Add Obsidian Tags": "Interpret #tags in the fields of a note as Anki tags, removing them from the note text in Anki.",
"Use Path as Deck": "Make anki's deck consistent with obsidian's folder structure"
"Use Path as Deck": "Make anki's deck consistent with obsidian's folder structure",
"Add Card link":"Add a link to be able to jump from anki to a block in obsidian",
}

export const DEFAULT_IGNORED_FILE_GLOBS = [
Expand Down Expand Up @@ -199,6 +200,9 @@ export class SettingsTab extends PluginSettingTab {
if (!(plugin.settings["Defaults"].hasOwnProperty("Use Path as Deck"))) {
plugin.settings["Defaults"]["Use Path as Deck"] = false
}
if (!(plugin.settings["Defaults"].hasOwnProperty("Add Card link"))) {
plugin.settings["Defaults"]["Add Card link"] = false
}
for (let key of Object.keys(plugin.settings["Defaults"])) {
// To account for removal of regex setting
if (key === "Regex") {
Expand Down

0 comments on commit 968c700

Please sign in to comment.