牙叔教程

  1. async function readEmbedFileContent(params, embed, parentFilePath) {
  2. let filePath = embed.link;
  3. filePath = parentFilePath + "/" + filePath;
  4. let normalizeFilePath = params.app.vault.adapter.path.posix.normalize(filePath);
  5. let file = params.app.vault.fileMap[normalizeFilePath];
  6. if (!file) {
  7. new Notice(`Could not find file ${filePath}`);
  8. console.error(`Could not find file ${filePath}`);
  9. return;
  10. }
  11. let mdEmbeds = getMdEmbedList(params, file);
  12. if (mdEmbeds.length == 0) {
  13. let fileContent = await params.app.vault.readRaw(filePath);
  14. return fileContent;
  15. } else {
  16. let fileContentArray = [];
  17. let positionStartOffset = 0;
  18. var len = mdEmbeds.length;
  19. let unsafeCachedData = file.unsafeCachedData;
  20. for (var i = 0; i < len; i++) {
  21. let embed = mdEmbeds[i];
  22. let position = getEmbedPosition(embed);
  23. let positionEndOffset = position.startOffset;
  24. let data = unsafeCachedData.substring(positionStartOffset, positionEndOffset);
  25. fileContentArray.push(data);
  26. positionStartOffset = position.endOffset;
  27. let currentParentFilePath = file.parent.path;
  28. let fileContent = await readEmbedFileContent(params, embed, currentParentFilePath);
  29. if (!fileContent) {
  30. continue;
  31. }
  32. fileContentArray.push(fileContent);
  33. }
  34. return fileContentArray.join("");
  35. }
  36. }
  37. function getEmbedPosition(embed) {
  38. let position = embed.position;
  39. let startOffset = position.start.offset;
  40. let endOffset = position.end.offset;
  41. return { startOffset, endOffset };
  42. }
  43. function getEmbedList(params, file) {
  44. let fileCache = params.app.metadataCache.getFileCache(file);
  45. return fileCache.embeds || [];
  46. }
  47. function getMdEmbedList(params, file) {
  48. let embeds = getEmbedList(params, file);
  49. let mdEmbeds = embeds.filter((embed) => {
  50. if (embed.link.endsWith(".md")) {
  51. return true;
  52. }
  53. return false;
  54. });
  55. return mdEmbeds;
  56. }
  57. module.exports = async (params) => {
  58. console.log("params.app.vault =");
  59. console.log(params.app.vault);
  60. const activeFile = params.app.workspace.getActiveFile();
  61. if (!activeFile) {
  62. new Notice("No active file.");
  63. return;
  64. }
  65. console.log("activeFile =");
  66. console.log(activeFile);
  67. let fileCache = await params.app.metadataCache.getFileCache(activeFile);
  68. console.log("fileCache =");
  69. console.log(fileCache);
  70. /* -------------------------------------------------------------------------- */
  71. let parentFilePath = activeFile.parent.path;
  72. let mdEmbeds = getMdEmbedList(params, activeFile);
  73. /* -------------------------------------------------------------------------- */
  74. // 读取文件内容
  75. // let fileContent = await params.app.vault.cachedRead(activeFile);
  76. let unsafeCachedData = activeFile.unsafeCachedData;
  77. let fileContentArray = [];
  78. let positionStartOffset = 0;
  79. var len = mdEmbeds.length;
  80. for (var i = 0; i < len; i++) {
  81. let embed = mdEmbeds[i];
  82. let position = getEmbedPosition(embed);
  83. let positionEndOffset = position.startOffset;
  84. let data = unsafeCachedData.substring(positionStartOffset, positionEndOffset);
  85. fileContentArray.push(data);
  86. positionStartOffset = position.endOffset;
  87. let fileContent = await readEmbedFileContent(params, embed, parentFilePath);
  88. if (!fileContent) {
  89. continue;
  90. }
  91. fileContentArray.push(fileContent);
  92. }
  93. let data = fileContentArray.join("");
  94. /* -------------------------------------------------------------------------- */
  95. let myDate = new Date();
  96. let str = myDate.toTimeString(); //"10:55:24 GMT+0800 (中国标准时间)"
  97. let timeStr = str.substring(0, 8); // '10:55:24'
  98. timeStr = "_" + timeStr.replace(/:/g, "_");
  99. /* -------------------------------------------------------------------------- */
  100. const path = `${parentFilePath}/${activeFile.basename + timeStr}.md`;
  101. if (data && !(await params.app.vault.adapter.exists(path))) {
  102. await params.app.vault.create(path, data);
  103. new Notice("Finished!");
  104. console.log("Finished!");
  105. } else {
  106. new Notice("No data to write.");
  107. console.log("No data to write.");
  108. }
  109. };

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问
—- 牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

bilibili

牙叔教程

微信公众号 牙叔教程

笔记的引用块转实际内容 - 图1

QQ群

747748653
笔记的引用块转实际内容 - 图2