From cb6973a41072b287aab7b8a1ee9e4aea1ed106f4 Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Mon, 17 Aug 2020 11:37:27 -0500 Subject: [PATCH 1/9] chore: updated entry point to app.js --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1d85e0b..a8d7f2c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "docsify-up", "version": "1.0.0", "description": "- https://documentup.com/jeromegn/documentup\r - https://github.com/docsifyjs/docsify/issues/217", - "main": "index.js", + "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, From 1ef60c5277ba46ec6c306932b79ae86756d083f5 Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Mon, 17 Aug 2020 11:38:52 -0500 Subject: [PATCH 2/9] feat: added support for custom fastdocs config (#5) --- app.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 5b9ff70..1eb22ee 100644 --- a/app.js +++ b/app.js @@ -7,11 +7,20 @@ const hasha = require("hasha"); const app = express(); const port = 3000; +let defaultConfig = { + name: "", + description: "Description", + plugins: [], + theme: "", +}; + app.get("/", (req, res) => res.send("Hello World!")); app.get("/:user/:repo/compile", (req, res) => { const user = req.params.user; const repo = req.params.repo; + defaultConfig.name = repo; + getReadme(user, repo).then(() => { res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); }); @@ -22,6 +31,12 @@ async function getReadme(user, repo) { const readme = await axios.get( `https://api.github.com/repos/${user}/${repo}/readme` ); + const gitRepo = await axios.get( + `https://api.github.com/repos/${user}/${repo}/contents` + ); + + const config = JSON.parse(await getConfig(gitRepo.data)); + console.log(config); const readmeUrl = readme.data.download_url; const readmeData = await axios.get(readmeUrl); @@ -42,7 +57,17 @@ async function getReadme(user, repo) { ); } - const htmlData = `${repo}
`; + let scriptPlugin = " "; + if (config.plugins.length > 0) { + config.plugins.forEach((plugin, index) => { + if (plugin == "docsify-copy-code") { + scriptPlugin += + ' '; + } + }); + } + + const htmlData = `${repo}
${scriptPlugin}`; fs.writeFileSync(`./docs/${user}-${repo}/.nojekyll`, "", function (err) { if (err) throw err; @@ -60,6 +85,22 @@ async function getReadme(user, repo) { await serveDocs(user, repo); } +async function getConfig(gitRepo) { + let configUrl = ""; + gitRepo.forEach((file, index) => { + if (file.name == ".fastdocs.json") { + configUrl = file.download_url; + } + }); + + if (configUrl != "") { + const config = await axios.get(configUrl); + return config.data; + } + + return defaultConfig; +} + async function isDiffReadme(user, repo, readmeData) { try { const localReadmeHash = hasha.fromFileSync( From c793ae4c01ce1873c28780c53763a0fc4cf171af Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Tue, 18 Aug 2020 16:12:57 -0500 Subject: [PATCH 3/9] chore: refactored function names, added basic error handling --- app.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app.js b/app.js index c3cb0d0..9c8a3ee 100644 --- a/app.js +++ b/app.js @@ -1,26 +1,26 @@ const express = require("express"); const util = require("./util"); + const app = express(); const port = 3000; -let defaultConfig = { - name: "", - description: "Description", - plugins: [], - theme: "", -}; - app.get("/", (req, res) => res.send("Hello World!")); app.get("/:user/:repo/compile", (req, res) => { const user = req.params.user; const repo = req.params.repo; - util.getReadme(user, repo).then(() => { - util.serveDocs(app, user, repo); - res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); - }); + try { + util.compileDocs(user, repo).then(() => { + util.serveDocs(app, user, repo); + res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); + }); + } catch (error) { + res.send( + `Oops. We couldn't render these docs. Check your config if you have one and try again!` + ); + } }); app.listen(port, () => From 6ed298b8823490d33574f4ecad32bdc06530d0fd Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Tue, 18 Aug 2020 16:13:39 -0500 Subject: [PATCH 4/9] feat: added config schema (#5) --- config.json | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..31d996b --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "description": "Description", + "plugins": [], + "gaCode": "" +} From 0a84ab78ef934a11b4149d19d2ccc9d273cac9d0 Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Tue, 18 Aug 2020 16:15:34 -0500 Subject: [PATCH 5/9] feat: added support for plugins (#2) --- docsify.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docsify.js b/docsify.js index aaafdb9..a05225b 100644 --- a/docsify.js +++ b/docsify.js @@ -1,5 +1,26 @@ +// Generates docsify enabled html with no config function generateHTML(user, repo) { return `${repo}
`; } -module.exports = { generateHTML }; +// Generated docsify enabled html with config and plugins +function generateHtmlWithConfig(config) { + if (config.enablePlugins) { + let ga = ""; // google analytics code + let search = ""; // search path + + if (config.plugins.ga) { + ga = `, ga: ${config.gaCode}`; + } + if (config.plugins.search) { + search = ', search: ["/"]'; + } + + return `${config.repo}
${config.plugins.tags}`; + } + + // If no plugins, but config exists for description + return `${config.repo}
`; +} + +module.exports = { generateHTML, generateHtmlWithConfig }; From b29c8192f0e3244cc3620fa4d319931c9c6259f8 Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Tue, 18 Aug 2020 16:16:33 -0500 Subject: [PATCH 6/9] feat: support for major docsify plugins (#4) --- plugins.js | 34 +++++++++++++++++++++ util.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 plugins.js diff --git a/plugins.js b/plugins.js new file mode 100644 index 0000000..7f27c2e --- /dev/null +++ b/plugins.js @@ -0,0 +1,34 @@ +function addPlugins(pluginList) { + let pluginConfig = { + ga: false, + search: false, + tags: "", + }; + let scriptTags = ""; + for (let i = 0; i < pluginList.length; i++) { + if (pluginList[i] == "docsify-copy-code") { + scriptTags += + ""; + } + if (pluginList[i] == "full-text-search") { + scriptTags += + ""; + pluginConfig.search = true; + } + if (pluginList[i] == "google-analytics") { + pluginConfig.ga = true; + } + if (pluginList[i] == "emoji") { + scriptTags += + ""; + } + if (pluginList[i] == "zoom-image") { + scriptTags += + ""; + } + } + pluginConfig.tags = scriptTags; + return pluginConfig; +} + +module.exports = { addPlugins }; diff --git a/util.js b/util.js index 1af1e16..127323f 100644 --- a/util.js +++ b/util.js @@ -4,6 +4,8 @@ const fs = require("fs"); const hasha = require("hasha"); const docsify = require("./docsify"); +const plugins = require("./plugins"); +let mainConfig = require("./config.json"); // Compares local and remote readme for changes function isDiffReadme(user, repo, readmeData) { @@ -22,7 +24,18 @@ function isDiffReadme(user, repo, readmeData) { } // Writes all the required files -function writeDocs(user, repo, readmeData) { +async function writeDocs(user, repo, readmeData) { + const config = await getConfig(user, repo); + + if (isConfigDefault(config)) { + writeDocsWithDefaultConfig(user, repo, readmeData); + } else { + // TODO: if config is not default? --> plugin integration!!! + writeDocsWithConfig(user, repo, readmeData, config); + } +} + +async function writeDocsWithDefaultConfig(user, repo, readmeData) { const htmlData = docsify.generateHTML(user, repo); fs.writeFileSync(`./docs/${user}-${repo}/README.md`, readmeData, function ( @@ -40,8 +53,47 @@ function writeDocs(user, repo, readmeData) { }); } +async function writeDocsWithConfig(user, repo, readmeData, config) { + try { + Object.keys(config).forEach((key) => { + mainConfig[`${key}`] = config[key]; + }); + + mainConfig.enablePlugins = false; + if (mainConfig.plugins.length > 0) { + mainConfig.enablePlugins = true; + } + + if (mainConfig.enablePlugins) { + const pluginConfig = plugins.addPlugins(mainConfig.plugins); + mainConfig.plugins = pluginConfig; + } + + const htmlData = docsify.generateHtmlWithConfig(mainConfig); + + fs.writeFileSync(`./docs/${user}-${repo}/README.md`, readmeData, function ( + err + ) { + if (err) throw err; + console.log("Generated ReadMe!"); + }); + + fs.writeFileSync(`./docs/${user}-${repo}/index.html`, htmlData, function ( + err + ) { + if (err) throw err; + console.log("Generated HTML!"); + }); + } catch (error) { + writeDocsWithDefaultConfig(user, repo, readmeData); + } +} + // Gets readme contents and generates /docs/ with docsify enabled to render async function getReadme(user, repo) { + mainConfig.user = user; + mainConfig.repo = repo; + const readme = await axios.get( `https://api.github.com/repos/${user}/${repo}/readme` ); @@ -58,7 +110,7 @@ async function getReadme(user, repo) { // Write file only if github readme is different from local copy if (isDiffReadme(user, repo, readmeData.data)) { - writeDocs(user, repo, readmeData.data); + await writeDocs(user, repo, readmeData.data); } } @@ -67,4 +119,34 @@ function serveDocs(app, user, repo) { app.use(`/${user}/${repo}`, express.static(`./docs/${user}-${repo}`)); } -module.exports = { getReadme, serveDocs }; +function isConfigDefault(config) { + if (config.name != "") return false; + return true; +} + +async function getConfig(user, repo) { + const gitRepo = await axios.get( + `https://api.github.com/repos/${user}/${repo}/contents` + ); + const gitRepoFiles = gitRepo.data; + let configUrl = ""; + + for (let i = 0; i < gitRepoFiles.length; i++) { + if (gitRepoFiles[i].name == ".fastdocs.json") { + configUrl = gitRepoFiles[i].download_url; + break; + } + } + + if (configUrl != "") { + const config = await axios.get(configUrl); + return config.data; + } + return mainConfig; +} + +async function compileDocs(user, repo) { + await getReadme(user, repo); +} + +module.exports = { compileDocs, serveDocs }; From 8743815ce65e141b8ea4090d95d278e1cd9d9fbc Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Wed, 19 Aug 2020 11:09:32 -0500 Subject: [PATCH 7/9] chore: refactor, removed try catch error handling (#14) --- app.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index 9c8a3ee..7255b0b 100644 --- a/app.js +++ b/app.js @@ -11,16 +11,10 @@ app.get("/:user/:repo/compile", (req, res) => { const user = req.params.user; const repo = req.params.repo; - try { - util.compileDocs(user, repo).then(() => { - util.serveDocs(app, user, repo); - res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); - }); - } catch (error) { - res.send( - `Oops. We couldn't render these docs. Check your config if you have one and try again!` - ); - } + util.compileDocs(user, repo).then(() => { + util.serveDocs(app, user, repo); + res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); + }); }); app.listen(port, () => From ab32a7092f15e1d487ed22ae7135c9e4d40db6ae Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Wed, 19 Aug 2020 11:11:42 -0500 Subject: [PATCH 8/9] chore: reduced conditional return statements (#14) --- docsify.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docsify.js b/docsify.js index a05225b..38d799f 100644 --- a/docsify.js +++ b/docsify.js @@ -5,22 +5,19 @@ function generateHTML(user, repo) { // Generated docsify enabled html with config and plugins function generateHtmlWithConfig(config) { - if (config.enablePlugins) { - let ga = ""; // google analytics code - let search = ""; // search path + let ga = ""; // google analytics code + let search = ""; // search path + if (config.enablePlugins) { if (config.plugins.ga) { ga = `, ga: ${config.gaCode}`; } if (config.plugins.search) { search = ', search: ["/"]'; } - - return `${config.repo}
${config.plugins.tags}`; } - // If no plugins, but config exists for description - return `${config.repo}
`; + return `${config.repo}
${config.plugins.tags}`; } module.exports = { generateHTML, generateHtmlWithConfig }; From eea68ff2608a67ba67d878fee27cb56c4a1690fd Mon Sep 17 00:00:00 2001 From: Anirudh Emmadi Date: Wed, 19 Aug 2020 11:14:51 -0500 Subject: [PATCH 9/9] style: updated logic to determine default config (#14) --- util.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/util.js b/util.js index 127323f..093326d 100644 --- a/util.js +++ b/util.js @@ -26,12 +26,10 @@ function isDiffReadme(user, repo, readmeData) { // Writes all the required files async function writeDocs(user, repo, readmeData) { const config = await getConfig(user, repo); - - if (isConfigDefault(config)) { - writeDocsWithDefaultConfig(user, repo, readmeData); - } else { - // TODO: if config is not default? --> plugin integration!!! + if (config) { writeDocsWithConfig(user, repo, readmeData, config); + } else { + writeDocsWithDefaultConfig(user, repo, readmeData); } } @@ -119,11 +117,6 @@ function serveDocs(app, user, repo) { app.use(`/${user}/${repo}`, express.static(`./docs/${user}-${repo}`)); } -function isConfigDefault(config) { - if (config.name != "") return false; - return true; -} - async function getConfig(user, repo) { const gitRepo = await axios.get( `https://api.github.com/repos/${user}/${repo}/contents` @@ -142,7 +135,7 @@ async function getConfig(user, repo) { const config = await axios.get(configUrl); return config.data; } - return mainConfig; + return ""; } async function compileDocs(user, repo) {