牙叔教程
async function readEmbedFileContent(params, embed, parentFilePath) {let filePath = embed.link;filePath = parentFilePath + "/" + filePath;let normalizeFilePath = params.app.vault.adapter.path.posix.normalize(filePath);let file = params.app.vault.fileMap[normalizeFilePath];if (!file) {new Notice(`Could not find file ${filePath}`);console.error(`Could not find file ${filePath}`);return;}let mdEmbeds = getMdEmbedList(params, file);if (mdEmbeds.length == 0) {let fileContent = await params.app.vault.readRaw(filePath);return fileContent;} else {let fileContentArray = [];let positionStartOffset = 0;var len = mdEmbeds.length;let unsafeCachedData = file.unsafeCachedData;for (var i = 0; i < len; i++) {let embed = mdEmbeds[i];let position = getEmbedPosition(embed);let positionEndOffset = position.startOffset;let data = unsafeCachedData.substring(positionStartOffset, positionEndOffset);fileContentArray.push(data);positionStartOffset = position.endOffset;let currentParentFilePath = file.parent.path;let fileContent = await readEmbedFileContent(params, embed, currentParentFilePath);if (!fileContent) {continue;}fileContentArray.push(fileContent);}return fileContentArray.join("");}}function getEmbedPosition(embed) {let position = embed.position;let startOffset = position.start.offset;let endOffset = position.end.offset;return { startOffset, endOffset };}function getEmbedList(params, file) {let fileCache = params.app.metadataCache.getFileCache(file);return fileCache.embeds || [];}function getMdEmbedList(params, file) {let embeds = getEmbedList(params, file);let mdEmbeds = embeds.filter((embed) => {if (embed.link.endsWith(".md")) {return true;}return false;});return mdEmbeds;}module.exports = async (params) => {console.log("params.app.vault =");console.log(params.app.vault);const activeFile = params.app.workspace.getActiveFile();if (!activeFile) {new Notice("No active file.");return;}console.log("activeFile =");console.log(activeFile);let fileCache = await params.app.metadataCache.getFileCache(activeFile);console.log("fileCache =");console.log(fileCache);/* -------------------------------------------------------------------------- */let parentFilePath = activeFile.parent.path;let mdEmbeds = getMdEmbedList(params, activeFile);/* -------------------------------------------------------------------------- */// 读取文件内容// let fileContent = await params.app.vault.cachedRead(activeFile);let unsafeCachedData = activeFile.unsafeCachedData;let fileContentArray = [];let positionStartOffset = 0;var len = mdEmbeds.length;for (var i = 0; i < len; i++) {let embed = mdEmbeds[i];let position = getEmbedPosition(embed);let positionEndOffset = position.startOffset;let data = unsafeCachedData.substring(positionStartOffset, positionEndOffset);fileContentArray.push(data);positionStartOffset = position.endOffset;let fileContent = await readEmbedFileContent(params, embed, parentFilePath);if (!fileContent) {continue;}fileContentArray.push(fileContent);}let data = fileContentArray.join("");/* -------------------------------------------------------------------------- */let myDate = new Date();let str = myDate.toTimeString(); //"10:55:24 GMT+0800 (中国标准时间)"let timeStr = str.substring(0, 8); // '10:55:24'timeStr = "_" + timeStr.replace(/:/g, "_");/* -------------------------------------------------------------------------- */const path = `${parentFilePath}/${activeFile.basename + timeStr}.md`;if (data && !(await params.app.vault.adapter.exists(path))) {await params.app.vault.create(path, data);new Notice("Finished!");console.log("Finished!");} else {new Notice("No data to write.");console.log("No data to write.");}};
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
—- 牙叔教程
声明
部分内容来自网络
本教程仅用于学习, 禁止用于其他用途
bilibili
微信公众号 牙叔教程

QQ群
747748653
