Skip to content

Commit

Permalink
Merge pull request #12 from MLH-Fellowship/dev
Browse files Browse the repository at this point in the history
Prototype Script
  • Loading branch information
aemmadi authored Aug 18, 2020
2 parents 6583e06 + 34901d4 commit 5c58e86
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ dist

# TernJS port file
.tern-port

# Misc.
.DS_Store

# Docs output
docs
12 changes: 12 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const express = require("express");

const util = require("./util");
const app = express();
const port = 3000;

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}`);
});
});

app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);
5 changes: 5 additions & 0 deletions docsify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function generateHTML(user, repo) {
return `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><title>${repo}</title><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="description" content="Description"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"/> <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css"/> </head> <body> <div id="app"></div><script>window.$docsify={name: '${repo}', repo: '${user}/${repo}'}; </script> <script src="//cdn.jsdelivr.net/npm/docsify@4"></script> </body></html>`;
}

module.exports = { generateHTML };
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}
},
"dependencies": {
"express": "^4.17.1"
"axios": "^0.19.2",
"express": "^4.17.1",
"hasha": "^5.2.0"
}
}
29 changes: 29 additions & 0 deletions template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="description" content="Description" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0"
/>
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css"
/>
</head>

<body>
<div id="app"></div>
<script>
window.$docsify = {
name: "",
repo: "",
};
</script>
<!-- Docsify v4 -->
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script>
</body>
</html>
70 changes: 70 additions & 0 deletions util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const express = require("express");
const axios = require("axios");
const fs = require("fs");
const hasha = require("hasha");

const docsify = require("./docsify");

// Compares local and remote readme for changes
function isDiffReadme(user, repo, readmeData) {
if (fs.existsSync(`./docs/${user}-${repo}/README.md`)) {
const localReadmeHash = hasha.fromFileSync(
`./docs/${user}-${repo}/README.md`,
{ algorithm: "md5" }
);
const remoteReadmeHash = hasha(readmeData, { algorithm: "md5" });

if (localReadmeHash == remoteReadmeHash) {
return false;
}
}
return true;
}

// Writes all the required files
function writeDocs(user, repo, readmeData) {
const htmlData = docsify.generateHTML(user, repo);

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!");
});
}

// Gets readme contents and generates /docs/<files> with docsify enabled to render
async function getReadme(user, repo) {
const readme = await axios.get(
`https://api.github.com/repos/${user}/${repo}/readme`
);
const readmeUrl = readme.data.download_url;
const readmeData = await axios.get(readmeUrl);

if (!fs.existsSync("./docs/")) {
fs.mkdirSync("./docs/");
}

if (!fs.existsSync(`./docs/${user}-${repo}/`)) {
fs.mkdirSync(`./docs/${user}-${repo}/`);
}

// Write file only if github readme is different from local copy
if (isDiffReadme(user, repo, readmeData.data)) {
writeDocs(user, repo, readmeData.data);
}
}

// Serves the compiled docs
function serveDocs(app, user, repo) {
app.use(`/${user}/${repo}`, express.static(`./docs/${user}-${repo}`));
}

module.exports = { getReadme, serveDocs };

0 comments on commit 5c58e86

Please sign in to comment.