Skip to content
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

Generate static HTML #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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({
Expand All @@ -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",
Expand Down
54 changes: 4 additions & 50 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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);

Expand Down Expand Up @@ -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);
};
Loading