diff options
author | Wouter Admiraal <wouter.admiraal@sonarsource.com> | 2019-01-08 11:01:45 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:42:57 +0100 |
commit | 505b1f5c8e6ee8f4f5376ffb7ad173dbd70c846b (patch) | |
tree | 7aca9e394521373602127c71b87f04961d6dfaf7 /server/sonar-docs/plugins | |
parent | 47068be19be3b7e80b27c34ccb9278c2e83ae9de (diff) | |
download | sonarqube-505b1f5c8e6ee8f4f5376ffb7ad173dbd70c846b.tar.gz sonarqube-505b1f5c8e6ee8f4f5376ffb7ad173dbd70c846b.zip |
DOC-131 Improve @include handling
Diffstat (limited to 'server/sonar-docs/plugins')
-rw-r--r-- | server/sonar-docs/plugins/sonarsource-source-filesystem/index.js | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/server/sonar-docs/plugins/sonarsource-source-filesystem/index.js b/server/sonar-docs/plugins/sonarsource-source-filesystem/index.js index 4ed6bec103d..7bf46ef2567 100644 --- a/server/sonar-docs/plugins/sonarsource-source-filesystem/index.js +++ b/server/sonar-docs/plugins/sonarsource-source-filesystem/index.js @@ -20,20 +20,23 @@ const { createFilePath, createRemoteFileNode } = require('gatsby-source-filesystem'); const fs = require('fs-extra'); -function loadNodeContent(fileNode) { +function loadNodeContentSync(fileNode) { const content = fs.readFileSync(fileNode.absolutePath, 'utf-8'); - return new Promise((resolve, reject) => { - let newContent = cutSonarCloudContent(content); - newContent = removeRemainingContentTags(newContent); - resolve(newContent); - }); + let newContent = cutSonarCloudContent(content); + newContent = removeRemainingContentTags(newContent); + newContent = handleIncludes(newContent, fileNode); + return newContent; +} + +function loadNodeContent(fileNode) { + return Promise.resolve(loadNodeContentSync(fileNode)); } function removeRemainingContentTags(content) { const regexBase = '<!-- \\/?(sonarqube|sonarcloud|static) -->'; return content - .replace(new RegExp(`^${regexBase}(\n|\r|\r\n|$)`, 'gm'), '') // First, remove single-line ones, including ending carriage-returns. - .replace(new RegExp(`${regexBase}`, 'g'), ''); // Now remove all remaining ones. + .replace(new RegExp(`^${regexBase}(\n|\r|\r\n|$)`, 'gm'), '') + .replace(new RegExp(`${regexBase}`, 'g'), ''); } function cutSonarCloudContent(content) { @@ -52,6 +55,27 @@ function cutSonarCloudContent(content) { return newContent; } +function handleIncludes(content, fileNode) { + return content.replace(/@include (.*)/g, (_, path) => { + const relativePath = `${path}.md`; + const absolutePath = `${__dirname}/../../src/${relativePath}`; + + if (relativePath === fileNode.relativePath) { + throw new Error(`Error in ${fileNode.relativePath}: The file is trying to include itself.`); + } else if (!fs.existsSync(absolutePath)) { + throw new Error( + `Error in ${fileNode.relativePath}: Couldn't load "${relativePath}" for inclusion.` + ); + } else { + const fileContent = loadNodeContentSync({ absolutePath, relativePath }); + return fileContent + .replace(/^---[\w\W]+?---$/m, '') + .replace(/^#+ *(toc|table[ -]of[ -]contents?)$/gim, '') + .trim(); + } + }); +} + exports.createFilePath = createFilePath; exports.createRemoteFileNode = createRemoteFileNode; exports.loadNodeContent = loadNodeContent; |