牙叔教程

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

名人名言

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

声明

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

bilibili

牙叔教程

微信公众号 牙叔教程

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

QQ群

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