Skip to content

Commit 6298033

Browse files
committed
Add source code publishing!
1 parent 2567fc5 commit 6298033

File tree

14 files changed

+147
-20
lines changed

14 files changed

+147
-20
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 16 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webdoc-cli/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ async function main(argv: yargs.Argv) {
141141
docDatabase: db,
142142
opts: config.opts,
143143
tutorials,
144+
source: sourceFiles,
144145
verbose: !!argv.verbose,
145146
};
146147

packages/webdoc-default-template/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
"code-prettify": "^0.1.0",
4848
"fs-extra": "^9.0.1",
4949
"markdown-it": "^11.0.0",
50-
"markdown-it-highlightjs": "^3.1.0"
50+
"markdown-it-highlightjs": "^3.1.0",
51+
"highlight.js": "~10.7.2"
5152
},
5253
"devDependencies": {
5354
"@babel/cli": "^7.8.4",

packages/webdoc-default-template/publish.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// @flow
22

33
const {crawl} = require("./helper/crawl");
4+
const fs = require("fs");
45
const fse = require("fs-extra");
6+
const hljs = require("highlight.js");
57
const path = require("path");
68
const {traverse} = require("@webdoc/model");
79
const {
@@ -24,6 +26,7 @@ const {categoryFilterPlugin} = require("./helper/renderer-plugins/category-filte
2426
import type {
2527
Doc,
2628
RootDoc,
29+
SourceFile,
2730
TutorialDoc,
2831
} from "@webdoc/types";
2932
@@ -62,6 +65,7 @@ let idToDoc/*: Map<string, Doc> */;
6265

6366
exports.publish = async function publish(options /*: PublishOptions */) {
6467
const config = options.config;
68+
const source = options.source;
6569

6670
await prepareLinker(config);
6771

@@ -146,6 +150,7 @@ exports.publish = async function publish(options /*: PublishOptions */) {
146150
});
147151

148152
await outStaticFiles(outDir, config);
153+
outSource(outDir, pipeline, options.config, source);
149154
outExplorerData(outDir, crawlData);
150155
outMainPage(indexRelative ? path.join(outDir, indexRelative) : null, pipeline, options.config);
151156
outIndexes(outDir, pipeline, options.config, crawlData.index);
@@ -247,6 +252,32 @@ async function outMainPage(
247252
}
248253
}
249254

255+
function outSource(
256+
outDir /*: string */,
257+
pipeline /*: TemplatePipeline */,
258+
config /*: ConfigSchema */,
259+
source /*: ?$ReadOnlyArray<SourceFile> */,
260+
) {
261+
if (source) {
262+
for (const file of source) {
263+
const raw = hljs.highlightAuto(
264+
fs.readFileSync(path.resolve(process.cwd(), file.path), "utf8"),
265+
).value;
266+
const pkgName = file.package.name || "";
267+
const outFile = path.join(pkgName, file.path + ".html");
268+
269+
pipeline.render("source.tmpl", {
270+
appBar: {current: "sources"},
271+
env: config,
272+
raw,
273+
title: path.basename(file.path),
274+
}, {
275+
outputFile: path.join(outDir, outFile),
276+
});
277+
}
278+
}
279+
}
280+
250281
async function outReadme(
251282
outputFile /*: string */,
252283
pipeline /*: TemplatePipeline */,
@@ -357,6 +388,7 @@ function outReference(
357388
document: doc,
358389
explorerData,
359390
title: doc.name,
391+
require,
360392
env: config,
361393
}, {
362394
outputFile: path.join(outDir, page),

packages/webdoc-default-template/src/styles/index.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@import "members";
1111
@import "members-explorer";
1212
@import "snippets";
13+
@import "source";
1314
@import "footer";
1415
@import "tutorial";
1516

packages/webdoc-default-template/src/styles/members.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
}
4646

4747
&_content {
48+
flex-grow: 1;
4849
font-size: 1.3em;
4950
margin-left: 8px;
5051
line-height: 1.4em;
@@ -95,6 +96,10 @@
9596
}
9697
}
9798

99+
.tag--source {
100+
font-size: 14px;
101+
}
102+
98103
.accordion {
99104
&__toggle {
100105
align-items: center;

packages/webdoc-default-template/src/styles/snippets.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
@import "vars";
44

5-
65
pre {
76
@include normalize();
87

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
.sources .page-content {
2+
max-width: 100%;
3+
}
4+
5+
.sources .page-content .source {
6+
margin: 24px 0 !important;
7+
}
8+
9+
.source {
10+
border: 1px solid #999;
11+
padding: 8px 8px 48px 8px;
12+
max-width: 100%;
13+
overflow-x: auto;
14+
15+
&__split {
16+
display: flex;
17+
flex-direction: row;
18+
19+
ul {
20+
font-size: 14px;
21+
line-height: 24px;
22+
height: 100%;
23+
list-style: none;
24+
padding-top: 24px;
25+
width: 48px;
26+
}
27+
}
28+
29+
pre {
30+
background-color: white !important;
31+
32+
code {
33+
background-color: white !important;
34+
font-size: 14px;
35+
line-height: 24px;
36+
}
37+
}
38+
}

packages/webdoc-default-template/static/styles/index.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webdoc-default-template/tmpl/components/member/index.tmpl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
/**
33
* Object member render component.
44
*/
5-
const doc = obj;
5+
const { doc, require } = obj;
66
const linkSvg = this.getPlugin("linker").getResourceURI("icons/link.svg");
7+
const pkgName = doc.loc && doc.loc.file && doc.loc.file.package && doc.loc.file.package.name || "";
8+
const source = doc.loc && doc.loc.file && doc.loc.file.path
9+
&& this.getPlugin("linker").getResourceURI(require("path").join(pkgName, doc.loc.file.path + ".html"));
10+
const sourceName = source && require("path").basename(doc.loc.file.path);
11+
const sourceStart = source && doc.loc.start ? ":" + doc.loc.start.line : "";
12+
const sourceLinkFragment = source && doc.loc.start ? "#" + doc.loc.start.line : "";
713

814
const modifiers = [
915
doc.deprecated ? "member__title_deprecated" : ""
@@ -25,6 +31,13 @@ const modifiers = [
2531
(deprecated)
2632
<?js } ?>
2733
</span>
34+
<?js if (source) { ?>
35+
<span class="tag--source">
36+
<a href="<?js= source + sourceLinkFragment ?>">
37+
<?js= sourceName + sourceStart ?>
38+
</a>
39+
</span>
40+
<?js } ?>
2841
</section>
2942
<?js } ?>
3043
<?js= this.partial("components/signature.tmpl", doc) ?>

0 commit comments

Comments
 (0)