diff --git a/app.js b/app.js index 3499e55..7255b0b 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,7 @@ const express = require("express"); const util = require("./util"); + const app = express(); const port = 3000; @@ -10,7 +11,7 @@ app.get("/:user/:repo/compile", (req, res) => { const user = req.params.user; const repo = req.params.repo; - util.getReadme(user, repo).then(() => { + util.compileDocs(user, repo).then(() => { util.serveDocs(app, user, repo); res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); }); 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": "" +} diff --git a/docsify.js b/docsify.js index aaafdb9..38d799f 100644 --- a/docsify.js +++ b/docsify.js @@ -1,5 +1,23 @@ +// 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) { + 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}`; +} + +module.exports = { generateHTML, generateHtmlWithConfig }; 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" }, 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..093326d 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,16 @@ 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 (config) { + writeDocsWithConfig(user, repo, readmeData, config); + } else { + writeDocsWithDefaultConfig(user, repo, readmeData); + } +} + +async function writeDocsWithDefaultConfig(user, repo, readmeData) { const htmlData = docsify.generateHTML(user, repo); fs.writeFileSync(`./docs/${user}-${repo}/README.md`, readmeData, function ( @@ -40,8 +51,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 +108,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 +117,29 @@ function serveDocs(app, user, repo) { app.use(`/${user}/${repo}`, express.static(`./docs/${user}-${repo}`)); } -module.exports = { getReadme, serveDocs }; +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 ""; +} + +async function compileDocs(user, repo) { + await getReadme(user, repo); +} + +module.exports = { compileDocs, serveDocs };