Last active
December 27, 2024 05:19
-
-
Save Moyf/f2fa6e88c589e70217188e0998e303f0 to your computer and use it in GitHub Desktop.
DVJS Code - Lite Database for Obsidian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useList = false; | |
const curNote = dv.current(); | |
if (!curNote){ | |
dv.span("The current document is not loaded, please reopen it."); | |
return; | |
} | |
let tarFile = await app.vault.getAbstractFileByPath(curNote.file.path); | |
// Get the meta data of the current file | |
const curFileMeta = app.metadataCache.getFileCache(tarFile); | |
const headings = curFileMeta.headings; | |
const headingsList = headings.map(k => k.heading) | |
// Get the text content of the current article | |
const curFilePath = curNote.file.path; | |
const curTFile = await app.vault.getFileByPath(curFilePath) | |
const content = await app.vault.cachedRead(curTFile); | |
if (!headings) { | |
dv.span("The current document lacks titles."); | |
return; | |
} | |
const meta = content.matchAll(/#+ (.*)\n(\[.*\:.*\])/gm); | |
// Use titles as keys and metadata as values to create a DV table | |
let tableHeads = ["Title"] | |
// Store as a dictionary first | |
let metaValues = [] | |
if (meta) { | |
for (let m of meta) { | |
let title = m[1] | |
// Check if the title is in the headings (to avoid treating content in code blocks as metadata) | |
if (headingsList.indexOf(title) == -1) { | |
continue | |
} | |
let metaList = m[2].split(" ") | |
let metaDict = {} | |
// Add metadata keys to the table header | |
for (let i = 0; i < metaList.length; i++) { | |
let keyValue = metaList[i].replace("[", "").replace("]", "").split("::") | |
// console.log(metaList[i], keyValue) | |
let key = keyValue[0].trim() | |
if (tableHeads.indexOf(key) == -1) { | |
tableHeads.push(key) | |
} | |
if (useList){ | |
// 1. List format, duplicate attributes will be merged | |
let value = [keyValue[1]] | |
metaDict[key] = metaDict[key] ? metaDict[key].concat(value) : value | |
} else { | |
// 2. Text format, duplicate attributes will use the latest | |
let value = keyValue[1] | |
metaDict[key] = metaDict[key] ? metaDict[key] + `,${value}` : value | |
} | |
} | |
let returnValuesList = [] | |
for (let i = 0; i < tableHeads.length; i++) { | |
if (i == 0) { | |
returnValuesList.push(`[[#${m[1]}]]`) | |
} else { | |
returnValuesList.push(metaDict[tableHeads[i]] ? metaDict[tableHeads[i]] : "") | |
} | |
} | |
metaValues.push(returnValuesList) | |
} | |
} | |
dv.table( | |
tableHeads, | |
metaValues.map(k => k) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useList = false; | |
const curNote = dv.current(); | |
if (!curNote){ | |
dv.span("当前文档未加载,请重新打开"); | |
return; | |
} | |
let tarFile = await app.vault.getAbstractFileByPath(curNote.file.path); | |
// 获取当前文件的 meta 数据 | |
const curFileMeta = app.metadataCache.getFileCache(tarFile); | |
const headings = curFileMeta.headings; | |
const headingsList = headings.map( k => k.heading ) | |
// 获取当前文章的文本内容 | |
const curFilePath = curNote.file.path; | |
const curTFile = await app.vault.getFileByPath(curFilePath) | |
const content = await app.vault.cachedRead(curTFile); | |
if (!headings) { | |
dv.span("当前文档缺少标题"); | |
return; | |
} | |
const meta = content.matchAll(/#+ (.*)\n(\[.*\:.*\])/gm); | |
// 标题作为 key,元数据作为 value,做成 DV 表格 | |
let tableHeads = ["标题"] | |
// 先存成一个 dict | |
let metaValues = [] | |
if (meta) { | |
for (let m of meta) { | |
let title = m[1] | |
// 检查 title 是否在 headings 内(避免把一些代码块内的内容也当作元数据) | |
if (headingsList.indexOf(title) == -1) { | |
continue | |
} | |
let metaList = m[2].split(" ") | |
let metaDict = {} | |
// 添加元数据的 key 到表头 | |
for (let i = 0; i < metaList.length; i++) { | |
let keyValue = metaList[i].replace("[", "").replace("]", "").split("::") | |
// console.log(metaList[i], keyValue) | |
let key = keyValue[0].trim() | |
if (tableHeads.indexOf(key) == -1) { | |
tableHeads.push(key) | |
} | |
if (useList){ | |
// 1. 列表形式,重复属性会合并 | |
let value = [keyValue[1]] | |
metaDict[key] = metaDict[key] ? metaDict[key].concat(value) : value | |
} else { | |
// 2. 文本形式,重复属性会使用最新的 | |
let value = keyValue[1] | |
metaDict[key] = metaDict[key] ? metaDict[key] + `,${value}` : value | |
} | |
} | |
let returnValuesList = [] | |
for (let i = 0; i < tableHeads.length; i++) { | |
if (i == 0) { | |
returnValuesList.push(`[[#${m[1]}]]`) | |
} else { | |
returnValuesList.push(metaDict[tableHeads[i]] ? metaDict[tableHeads[i]] : "") | |
} | |
} | |
metaValues.push(returnValuesList) | |
} | |
} | |
dv.table( | |
tableHeads, | |
metaValues.map( k => k ) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment