Skip to content

Generate static HTML #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion bin/docsify
Original file line number Diff line number Diff line change
@@ -86,7 +86,7 @@ const yargs = require("yargs")
handler: argv => run.serve(argv.path, argv.open, argv.port, argv.P, argv.i)
})
.command({
command: "start <path>",
command: "start [path]",
desc: chalk.gray(y18n.__("start")),
builder: yargs =>
yargs.options({
@@ -109,6 +109,38 @@ const yargs = require("yargs")
}),
handler: argv => run.start(argv.path, argv.config, argv.port)
})
.command({
command: "static [path]",
desc: chalk.gray(y18n.__("static")),
builder: yargs =>
yargs.options({
dest: {
alias: "d",
default: "docs-static",
desc: chalk.gray(y18n.__("static.dest")),
nargs: 0,
requiresArg: false,
type: "string"
},
config: {
alias: "c",
default: false,
desc: chalk.gray(y18n.__("static.config")),
nargs: 0,
requiresArg: false,
type: "string"
},
"index-name": {
alias: "i",
desc: chalk.gray(y18n.__("serve.indexname")),
nargs: 1,
requiresArg: true,
type: "string"
}
}),
handler: argv =>
run.static(argv.path, argv.config, argv.indexName, argv.dest)
})
.help()
.option("help", {
alias: "h",
54 changes: 4 additions & 50 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
@@ -3,67 +3,21 @@
const connect = require("connect");
const serveStatic = require("serve-static");
const Renderer = require("docsify-server-renderer");
const fs = require("fs");
const util = require("../util/index");
const chalk = require("chalk");
const LRU = require("lru-cache");

const defaultConfig = {
template: `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Doc</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
</head>
<body>
<!--inject-app-->
<!--inject-config-->
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
</body>
</html>`
};

function loadConfig(config) {
try {
return require(util.cwd(config));
} catch (e) {
console.log(chalk.red(`${e.message} in ${config}`));
process.exit(1);
}
}

module.exports = function(path, configFile, port) {
let config = defaultConfig;
const pkg = util.pkg();
const ctx = util.cwd(".");

path = path || "./";

if (configFile) {
config = loadConfig(configFile);
config.template = /\.html$/.test(config.template)
? util.read(util.resolve(ctx, config.template))
: defaultConfig.template;
} else if (pkg.docsify) {
const tpl = pkg.docsify.template;
const config = util.getConfig(configFile);

config = pkg.docsify;
config.template =
tpl && util.exists(util.resolve(ctx, tpl))
? util.read(tpl)
: defaultConfig.template;
}
path = path || ".";

const renderer = new Renderer(Object.assign(defaultConfig, config));
const renderer = new Renderer(config);
const server = connect();
const cached = new LRU(config.maxAge || 0);

server.use(serveStatic(path));
server.use(function(req, res) {
serveStatic(path)(req, res, function() {
serveStatic(path, { index: false })(req, res, function() {
if (
/\.(jpg|jpeg|gif|png|svg|ico|mp4|webm|ogg|ogv|js|css|md)(?:\?v=[0-9.]+)?$/.test(
req.url
74 changes: 74 additions & 0 deletions lib/commands/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"use strict";

const serveStatic = require("serve-static");
const connect = require("connect");
const getPort = require("get-port");
const Renderer = require("docsify-server-renderer");
const util = require("../util/index");
const fg = require("fast-glob");
const write = require("write");
const ora = require("ora");
const chalk = require("chalk");
const fse = require("fs-extra");

const resolve = util.resolve;

module.exports = function(
path = "",
configFile,
indexName,
dest = "docs-static"
) {
path = resolve(path || ".");
const config = util.getConfig(configFile);
// docs absolute path
config.path = path;

const server = connect();
const indexFile = resolve(path, indexName || "index.html");
const render = new Renderer(config);
const files = fg.sync("**/*.md", {
cwd: path,
ignore: ["_*"]
});
let app;
const spinner = ora();

spinner.start("Rendering");

getPort()
.then(async port => {
server.use(serveStatic(path, { index: indexFile }));
app = server.listen(port);

const queue = [];
for (const file of files) {
const res = await render.render(file);
queue.push(res);
console.log("Rendering " + file);
}
return queue;
})
.then(data => {
// save file
return Promise.all(
data.map(({ url, content, path }) => {
const filePath = url
.replace(/README\.md$/, "index.html")
.replace(/\.md$/, ".html");
write(resolve(util.cwd(), dest, filePath), content);
})
);
})
.then(() => {
return fse.copy(path, dest);
})
.then(() => {
spinner.succeed(`Success! Generate static files at ${chalk.green(dest)}`);
app && app.close();
})
.catch(e => {
spinner.fail(e.message);
app && app.close();
});
};
17 changes: 17 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
exports.SSR_DEFAULT = {
template: `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Doc</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
</head>
<body>
<!--inject-app-->
<!--inject-config-->
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
</body>
</html>`
};
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
init: require('./commands/init'),
serve: require('./commands/serve'),
start: require('./commands/start')
}
init: require("./commands/init"),
serve: require("./commands/serve"),
start: require("./commands/start"),
static: require("./commands/static")
};
34 changes: 34 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

const fs = require("fs");
const SSR_DEFAULT = require("../config").SSR_DEFAULT;
const chalk = require("chalk");

const resolve = (exports.resolve = require("path").resolve);

@@ -28,3 +30,35 @@ exports.pkg = function() {
exports.read = function(path) {
return fs.readFileSync(path, "utf-8").toString();
};

function loadConfig(config) {
try {
return require(exports.cwd(config));
} catch (e) {
console.log(chalk.red(`${e.message} in ${config}`));
process.exit(1);
}
}

exports.getConfig = function(configFile) {
const pkg = exports.pkg();
const ctx = exports.cwd(".");
let config = SSR_DEFAULT;

if (configFile) {
config = loadConfig(configFile);
config.template = /\.html$/.test(config.template)
? read(resolve(ctx, config.template))
: SSR_DEFAULT.template;
} else if (pkg.docsify) {
const tpl = pkg.docsify.template;

config = pkg.docsify;
config.template =
tpl && exports.exists(resolve(ctx, tpl))
? exports.read(tpl)
: SSR_DEFAULT.template;
}

return Object.assign(SSR_DEFAULT, config);
};
514 changes: 474 additions & 40 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -31,15 +31,18 @@
"connect": "^3.6.0",
"connect-livereload": "^0.6.0",
"cp-file": "^4.1.1",
"docsify": ">=3",
"docsify-server-renderer": ">=4",
"docsify": ">=4",
"docsify-server-renderer": "^4.9.0",
"fast-glob": "^2.2.6",
"fs-extra": "^2.1.2",
"get-port": "^4.1.0",
"livereload": "^0.7.0",
"lru-cache": "^4.1.1",
"opn": "^5.3.0",
"ora": "^3.1.0",
"serve-static": "^1.12.1",
"update-notifier": "^2.1.0",
"write": "^1.0.3",
"y18n": "^3.2.1",
"yargonaut": "^1.1.2",
"yargs": "^7.0.2"
5 changes: 4 additions & 1 deletion tools/locales/de.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"epilog": "Dokumentation:\n https://QingWei-Li.github.io/docsify\n https://QingWei-Li.github.io/docsify-cli\n\nEntwicklung:\n https://github.com/QingWei-Li/docsify-cli/blob/master/CONTRIBUTING.md\n",
"epilog": "Dokumentation:\n https://docsify.js.org\n https://docsifyjs.github.io/docsify-cli\n\nEntwicklung:\n https://github.com/docsifyjs/docsify-cli/blob/master/CONTRIBUTING.md\n",
"group.globaloptions": "Globale Optionen",
"help": "Zeige Hilfe an",
"init": "Erzeuge neue Dokumentation.",
@@ -10,6 +10,9 @@
"serve.open": "Dokumentation im Standardbrowser öffnen. Um explizit --open auf false zu setzen, kannst du --no-open verwenden.",
"serve.port": "Listen port.",
"serve.indexname": "Custom filename instead of index.html to serve by default",
"static": "Generate static HTML",
"static.dest": "Output directory",
"static.config": "docsify config file",
"livereload.port": "livereload Listen port.",
"usage": "Anwendung",
"version": "Zeige Versionsnummer an"
5 changes: 4 additions & 1 deletion tools/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"epilog": "Documentation:\n https://QingWei-Li.github.io/docsify\n https://QingWei-Li.github.io/docsify-cli\n\nDevelopment:\n https://github.com/QingWei-Li/docsify-cli/blob/master/CONTRIBUTING.md\n",
"epilog": "Documentation:\n https://docsify.js.org\n https://docsifyjs.github.io/docsify-cli\n\nDevelopment:\n https://github.com/docsifyjs/docsify-cli/blob/master/CONTRIBUTING.md\n",
"group.globaloptions": "Global Options",
"help": "Show help",
"init": "Creates new docs",
@@ -10,6 +10,9 @@
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
"serve.port": "Listen port.",
"serve.indexname": "Custom filename instead of index.html to serve by default",
"static": "Generate static HTML",
"static.dest": "Output directory",
"static.config": "docsify config file",
"livereload.port": "livereload Listen port.",
"usage": "Usage",
"version": "Show version number"
5 changes: 4 additions & 1 deletion tools/locales/zh.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"epilog": "文档地址:\n https://QingWei-Li.github.io/docsify\n https://QingWei-Li.github.io/docsify-cli\n\n开发:\n https://github.com/QingWei-Li/docsify-cli/blob/master/CONTRIBUTING.md\n",
"epilog": "文档地址:\n https://docsify.js.org\n https://docsifyjs.github.io/docsify-cli\n\n开发:\n https://github.com/docsifyjs/docsify-cli/blob/master/CONTRIBUTING.md\n",
"group.globaloptions": "全局选项",
"help": "帮助",
"start": "Server for SSR",
@@ -10,6 +10,9 @@
"serve.open": "自动打开浏览器",
"serve.port": "设置端口",
"serve.indexname": "Custom filename instead of index.html to serve by default",
"static": "生成静态 HTML",
"static.dest": "输出目录",
"static.config": "配置文件",
"livereload.port": "设置livereload端口",
"usage": "例子",
"version": "当前版本号"