diff --git a/.obsidian/vault-stats.json b/.obsidian/vault-stats.json index 38a4a73..2bbce08 100644 --- a/.obsidian/vault-stats.json +++ b/.obsidian/vault-stats.json @@ -1 +1 @@ -{"history":{"2023-03-01":{"words":845,"characters":1255,"sentences":0,"files":123,"totalWords":29296,"totalCharacters":231518,"totalSentences":821},"2023-03-02":{"words":39,"characters":281,"sentences":0,"files":123,"totalWords":29296,"totalCharacters":231518,"totalSentences":821},"2023-03-03":{"words":644,"characters":3897,"sentences":23,"files":123,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-05":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-06":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-07":{"words":16,"characters":103,"sentences":0,"files":123,"totalWords":29956,"totalCharacters":233253,"totalSentences":844},"2023-03-08":{"words":6,"characters":47,"sentences":1,"files":123,"totalWords":29964,"totalCharacters":233304,"totalSentences":845},"2023-03-10":{"words":15,"characters":102,"sentences":0,"files":124,"totalWords":30010,"totalCharacters":233650,"totalSentences":846},"2023-03-14":{"words":68,"characters":445,"sentences":4,"files":126,"totalWords":30274,"totalCharacters":235667,"totalSentences":857},"2023-03-15":{"words":585,"characters":3851,"sentences":9,"files":129,"totalWords":30998,"totalCharacters":240608,"totalSentences":868},"2023-03-16":{"words":128,"characters":820,"sentences":7,"files":131,"totalWords":31352,"totalCharacters":245878,"totalSentences":877},"2023-03-18":{"words":30,"characters":208,"sentences":0,"files":132,"totalWords":31501,"totalCharacters":247094,"totalSentences":878},"2023-03-19":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":0,"totalCharacters":0,"totalSentences":0},"2023-03-22":{"words":2568,"characters":16916,"sentences":53,"files":133,"totalWords":34073,"totalCharacters":266611,"totalSentences":932},"2023-03-24":{"words":7,"characters":50,"sentences":0,"files":133,"totalWords":34085,"totalCharacters":264035,"totalSentences":932},"2023-03-30":{"words":156,"characters":955,"sentences":9,"files":135,"totalWords":34782,"totalCharacters":265893,"totalSentences":965},"2023-03-31":{"words":129,"characters":798,"sentences":7,"files":135,"totalWords":34911,"totalCharacters":269322,"totalSentences":972},"2023-04-03":{"words":9,"characters":64,"sentences":0,"files":142,"totalWords":35205,"totalCharacters":270666,"totalSentences":988,"totalFootnotes":3,"totalCitations":0,"totalPages":117.19999999999997,"footnotes":0,"citations":0,"pages":0}},"modifiedFiles":{"Aventures/Chapitre 1 - Révolution.md":{"words":{"initial":296,"current":295},"characters":{"initial":2374,"current":2369},"sentences":{"initial":8,"current":8},"footnotes":{"initial":0,"current":0},"citations":{"initial":0,"current":0},"pages":{"initial":1,"current":1}},"Projets/Absolue Nécessitée/Scenes/Diner chez Althéa.md":{"words":{"initial":0,"current":8},"characters":{"initial":1,"current":61},"sentences":{"initial":1,"current":1},"footnotes":{"initial":0,"current":0},"citations":{"initial":0,"current":0},"pages":{"initial":0,"current":0}},"Projets/Absolue Nécessitée/Scenes/Assassinat raté.md":{"words":{"initial":0,"current":1},"characters":{"initial":1,"current":5},"sentences":{"initial":1,"current":1},"footnotes":{"initial":0,"current":0},"citations":{"initial":0,"current":0},"pages":{"initial":0,"current":0}},"Scenes/Diner avec Althéa.md":{"words":{"initial":711,"current":642},"characters":{"initial":4765,"current":3875},"sentences":{"initial":25,"current":24},"footnotes":{"initial":0,"current":0},"citations":{"initial":0,"current":0},"pages":{"initial":2.4,"current":2.1}}}} \ No newline at end of file +{"history":{"2023-03-01":{"words":845,"characters":1255,"sentences":0,"files":123,"totalWords":29296,"totalCharacters":231518,"totalSentences":821},"2023-03-02":{"words":39,"characters":281,"sentences":0,"files":123,"totalWords":29296,"totalCharacters":231518,"totalSentences":821},"2023-03-03":{"words":644,"characters":3897,"sentences":23,"files":123,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-05":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-06":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":29940,"totalCharacters":233149,"totalSentences":844},"2023-03-07":{"words":16,"characters":103,"sentences":0,"files":123,"totalWords":29956,"totalCharacters":233253,"totalSentences":844},"2023-03-08":{"words":6,"characters":47,"sentences":1,"files":123,"totalWords":29964,"totalCharacters":233304,"totalSentences":845},"2023-03-10":{"words":15,"characters":102,"sentences":0,"files":124,"totalWords":30010,"totalCharacters":233650,"totalSentences":846},"2023-03-14":{"words":68,"characters":445,"sentences":4,"files":126,"totalWords":30274,"totalCharacters":235667,"totalSentences":857},"2023-03-15":{"words":585,"characters":3851,"sentences":9,"files":129,"totalWords":30998,"totalCharacters":240608,"totalSentences":868},"2023-03-16":{"words":128,"characters":820,"sentences":7,"files":131,"totalWords":31352,"totalCharacters":245878,"totalSentences":877},"2023-03-18":{"words":30,"characters":208,"sentences":0,"files":132,"totalWords":31501,"totalCharacters":247094,"totalSentences":878},"2023-03-19":{"words":0,"characters":0,"sentences":0,"files":0,"totalWords":0,"totalCharacters":0,"totalSentences":0},"2023-03-22":{"words":2568,"characters":16916,"sentences":53,"files":133,"totalWords":34073,"totalCharacters":266611,"totalSentences":932},"2023-03-24":{"words":7,"characters":50,"sentences":0,"files":133,"totalWords":34085,"totalCharacters":264035,"totalSentences":932},"2023-03-30":{"words":156,"characters":955,"sentences":9,"files":135,"totalWords":34782,"totalCharacters":265893,"totalSentences":965},"2023-03-31":{"words":129,"characters":798,"sentences":7,"files":135,"totalWords":34911,"totalCharacters":269322,"totalSentences":972},"2023-04-03":{"words":9,"characters":64,"sentences":0,"files":142,"totalWords":35205,"totalCharacters":270666,"totalSentences":988,"totalFootnotes":3,"totalCitations":0,"totalPages":117.19999999999997,"footnotes":0,"citations":0,"pages":0},"2023-04-04":{"words":0,"characters":0,"sentences":0,"pages":0,"files":0,"footnotes":0,"citations":0,"totalWords":35205,"totalCharacters":270666,"totalSentences":988,"totalFootnotes":3,"totalCitations":0,"totalPages":117.19999999999997}},"modifiedFiles":{}} \ No newline at end of file diff --git a/Projets/Scripts/replaceLinks.js b/Projets/Scripts/replaceLinks.js index 682d5e1..62e8932 100644 --- a/Projets/Scripts/replaceLinks.js +++ b/Projets/Scripts/replaceLinks.js @@ -66,6 +66,7 @@ module.exports = { */ compile }; + async function compile(input, context) { const files = await getAllFiles(context.app); console.info("All files: ", files); @@ -78,19 +79,8 @@ async function compile(input, context) { ) } -async function getAllFiles(app) { - const vaultDir = app.vault.adapter.basePath; - const fileData = await app.vault.adapter.fs.readdir(vaultDir, { withFileTypes: true }); - - const files = await Promise.all(fileData.map(async (file) => { - const filePath = path.join(vaultDir, file.name); - if (file.isFile() && path.extname(filePath) === '.md') { - const contents = await app.vault.read(file.name); - return { name: file.name, contents }; - } - })); - - return files.filter(file => file !== undefined); +function getAllFiles(app) { + return app.vault.getFiles(); } async function replaceLinksWithContent(content, metadata, context, files, deep) { @@ -103,49 +93,48 @@ async function replaceLinksWithContent(content, metadata, context, files, deep) return allLinks.reduce(async (accProm, item) => { const acc = await accProm; - - // Extract the link text and the link path from the link - const linkText = item.link; - const linkPath = path.isAbsolute(linkText) ? linkText.slice(1) : path.join(path.dirname(metadata.path), linkText); - - const [linkedFile, linkedContent] = await getLinkedContent(linkPath, files); + const [linkedFile, linkedContent] = await getLinkedContent(item.link, context.app, files); if (!linkedContent) { return acc; } - - const linkedMetadata = getMetadata(linkedFile.name, context.app); - + const linkedMetadata = getMetadata(linkedFile, context.app); + let contentArray = Array.from(linkedContent); contentArray = removeFrontmatter(contentArray, linkedMetadata.frontmatter, context.optionValues); - contentArray = removeHeaders(contentArray, linkedMetadata.headings, context.optionValues); + contentArray = removeHeaders(contentArray, linkedMetadata.headings, context.optionValues) const cleanedContent = contentArray.filter(val => val !== null) - .join("") - .trim(); + .join("") + .trim(); - const processedLinkedContent = await replaceLinksWithContent(cleanedContent, linkedMetadata, context, files, deep + 1); - return acc.replaceAll(item.original, processedLinkedContent); + const processedLinkedContent = await replaceLinksWithContent(cleanedContent, linkedMetadata, context, files, deep + 1) + return acc.replaceAll(item.original, processedLinkedContent) }, Promise.resolve(content)); } -async function getLinkedContent(linkPath, files) { - const linkedFile = files.find(file => file.name === linkPath); - return linkedFile - ? [linkedFile, linkedFile.contents] +async function getLinkedContent(linkText, app, files) { + const file = files.find(file => file.basename === linkText); + if(!file){ + const file = files.find(file => file.path === linkText); + } + if(!file){ + const file = files.find(file => file.basename === getFilenameWithoutExtension(linkText)); + } + return file + ? [file, await app.vault.read(file)] : []; } -function getMetadata(fileName, app) { - const file = app.metadataCache.getFirstLinkpathDest(fileName, ''); - return app.metadataCache.getFileCache(file); +function getMetadata(file, app) { + return app.metadataCache.getFileCache(file) } -function removeHeaders(content, headings, optionValues) { - if (optionValues.removeH1 && headings) { - return headings +function removeHeaders(content, headers, optionValues) { + if (optionValues.removeH1 && headers) { + return headers .filter(header => header.level === 1) .reduce((acc, header) => { - return removeFromTextArray(acc, header.position); - }, content); + return removeFromTextArray(acc, header.position) + }, content) } else { return content; } @@ -160,5 +149,10 @@ function removeFrontmatter(content, frontmatter, optionValues) { } function removeFromTextArray(text, position) { - return text.map((val, i) => i >= position.start.offset && i < position.end.offset ? null : val); + return text.map((val, i) => i >= position.start.offset && i < position.end.offset ? null : val) } + +function getFilenameWithoutExtension(filePath) { + const fileName = filePath.split('/').pop(); + return fileName.split('.').slice(0, -1).join('.'); +} \ No newline at end of file