diff options
author | Pascal Mugnier <pascal.mugnier@sonarsource.com> | 2018-04-26 08:42:19 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2018-05-03 20:20:50 +0200 |
commit | b083d4376580b8dd252933025ca86ce5b98ce7af (patch) | |
tree | a0d05a812d6c70c3d8573106b12a1ec74f6faefa /server/sonar-web/config | |
parent | 0c996218c1a2c2542c3465a7bf1f38ee386132da (diff) | |
download | sonarqube-b083d4376580b8dd252933025ca86ce5b98ce7af.tar.gz sonarqube-b083d4376580b8dd252933025ca86ce5b98ce7af.zip |
SONAR-10612 Create documentation space in the web app
Diffstat (limited to 'server/sonar-web/config')
-rw-r--r-- | server/sonar-web/config/documentation-loader/fetch-matter.js | 44 | ||||
-rw-r--r-- | server/sonar-web/config/documentation-loader/index.js | 39 | ||||
-rw-r--r-- | server/sonar-web/config/documentation-loader/parse-directory.js | 24 | ||||
-rw-r--r-- | server/sonar-web/config/paths.js | 4 | ||||
-rw-r--r-- | server/sonar-web/config/webpack.config.js | 16 |
5 files changed, 124 insertions, 3 deletions
diff --git a/server/sonar-web/config/documentation-loader/fetch-matter.js b/server/sonar-web/config/documentation-loader/fetch-matter.js new file mode 100644 index 00000000000..657a07f08e9 --- /dev/null +++ b/server/sonar-web/config/documentation-loader/fetch-matter.js @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +const fs = require('fs'); +const path = require('path'); +const matter = require('gray-matter'); + +const compare = (a, b) => { + if (a.order === b.order) return a.title.localeCompare(b.title); + if (a.order === -1) return 1; + if (b.order === -1) return -1; + return a.order - b.order; +}; + +module.exports = (root, files) => { + return files + .map(file => { + const content = fs.readFileSync(root + '/' + file, 'utf8'); + const headerData = matter(content).data; + return { + name: path.basename(file).slice(0, -3), + relativeName: file.slice(0, -3), + title: headerData.title || file, + order: headerData.order || -1 + }; + }) + .sort(compare); +}; diff --git a/server/sonar-web/config/documentation-loader/index.js b/server/sonar-web/config/documentation-loader/index.js new file mode 100644 index 00000000000..d25477bedd9 --- /dev/null +++ b/server/sonar-web/config/documentation-loader/index.js @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +const path = require('path'); +const parseDirectory = require('./parse-directory'); +const fetchMatter = require('./fetch-matter'); + +module.exports = function(source) { + this.cacheable(); + + const failure = this.async(); + const success = failure.bind(null, null); + + const config = this.exec(source, this.resourcePath); + const root = path.resolve(path.dirname(this.resourcePath), config.root); + this.addContextDependency(root); + + parseDirectory(root) + .then(files => fetchMatter(root, files)) + .then(result => `module.exports = ${JSON.stringify(result)};`) + .then(success) + .catch(failure); +}; diff --git a/server/sonar-web/config/documentation-loader/parse-directory.js b/server/sonar-web/config/documentation-loader/parse-directory.js new file mode 100644 index 00000000000..35b37040d72 --- /dev/null +++ b/server/sonar-web/config/documentation-loader/parse-directory.js @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +const glob = require('glob-promise'); + +module.exports = root => { + return glob(root + '/**/*.md').then(files => files.map(file => file.substr(root.length + 1))); +}; diff --git a/server/sonar-web/config/paths.js b/server/sonar-web/config/paths.js index 9733ef712ff..d1acbdd2274 100644 --- a/server/sonar-web/config/paths.js +++ b/server/sonar-web/config/paths.js @@ -22,5 +22,7 @@ const path = require('path'); module.exports = { appBuild: path.join(__dirname, '../build/webapp'), appPublic: path.join(__dirname, '../public'), - appHtml: path.join(__dirname, '../public/index.html') + appHtml: path.join(__dirname, '../public/index.html'), + docRoot: path.join(__dirname, '../../sonar-docs/src'), + docImages: path.join(__dirname, '../../sonar-docs/src/images') }; diff --git a/server/sonar-web/config/webpack.config.js b/server/sonar-web/config/webpack.config.js index cc2bfe7e970..c5439551c5c 100644 --- a/server/sonar-web/config/webpack.config.js +++ b/server/sonar-web/config/webpack.config.js @@ -36,7 +36,7 @@ module.exports = ({ production = true }) => ({ extensions: ['.ts', '.tsx', '.js', '.json'], // import from 'Docs/foo.md' is rewritten to import from 'sonar-docs/src/foo.md' alias: { - Docs: path.resolve(__dirname, '../../sonar-docs/src/tooltips') + Docs: path.resolve(__dirname, '../../sonar-docs/src') } }, entry: [ @@ -79,7 +79,11 @@ module.exports = ({ production = true }) => ({ }, { test: require.resolve('lodash'), loader: 'expose-loader?_' }, { test: require.resolve('react'), loader: 'expose-loader?React' }, - { test: require.resolve('react-dom'), loader: 'expose-loader?ReactDOM' } + { test: require.resolve('react-dom'), loader: 'expose-loader?ReactDOM' }, + { + test: /\.directory-loader\.js$/, + loader: path.resolve(__dirname, 'documentation-loader/index.js') + } ].filter(Boolean) }, plugins: [ @@ -89,6 +93,14 @@ module.exports = ({ production = true }) => ({ production && new CopyWebpackPlugin([ { + from: paths.docImages, + to: paths.appBuild + '/images/embed-doc/images' + } + ]), + + production && + new CopyWebpackPlugin([ + { from: paths.appPublic, to: paths.appBuild, ignore: [paths.appHtml] |