forked from facebook/docusaurus
/
docs.js
165 lines (152 loc) · 4.79 KB
/
docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const CWD = process.cwd();
const siteConfig = require(`${CWD}/siteConfig.js`);
const {join} = require('path');
const fs = require('fs-extra');
const React = require('react');
const env = require('./env.js');
const {renderToStaticMarkupWithDoctype} = require('./renderUtils');
const readMetadata = require('./readMetadata.js');
const {insertTOC} = require('../core/toc.js');
const {getPath} = require('../core/utils.js');
function getFilePath(metadata) {
if (!metadata) {
return null;
}
let file;
if (env.versioning.enabled && metadata.original_id) {
if (env.translation.enabled && metadata.language !== 'en') {
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
} else {
file = join(CWD, 'versioned_docs', metadata.source);
}
} else if (env.translation.enabled && metadata.language !== 'en') {
file = join(CWD, 'translated_docs', metadata.language, metadata.source);
} else {
file = join(CWD, '..', readMetadata.getDocsPath(), metadata.source);
}
return file;
}
function getFile(metadata) {
if (!metadata) {
return null;
}
const file = getFilePath(metadata);
if (!fs.existsSync(file)) {
return null;
}
return fs.readFileSync(file, 'utf8');
}
function mdToHtmlify(oldContent, mdToHtml, metadata) {
let content = oldContent;
const mdLinks = [];
const mdReferences = [];
// find any links to markdown files
const linkRegex = /(?:\]\()(?:\.\/)?([^'")\]\s>]+\.md)/g;
let linkMatch = linkRegex.exec(content);
while (linkMatch !== null) {
mdLinks.push(linkMatch[1]);
linkMatch = linkRegex.exec(content);
}
// find any reference links to markdown files
const refRegex = /(?:\]:)(?:\s)?(?:\.\/|\.\.\/)?([^'")\]\s>]+\.md)/g;
let refMatch = refRegex.exec(content);
while (refMatch !== null) {
mdReferences.push(refMatch[1]);
refMatch = refRegex.exec(content);
}
// replace markdown links to their website html links
new Set(mdLinks).forEach(mdLink => {
let htmlLink = mdToHtml[mdLink];
if (htmlLink) {
htmlLink = getPath(htmlLink, siteConfig.cleanUrl);
htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`);
htmlLink = htmlLink.replace(
'/VERSION/',
metadata.version && metadata.version !== env.versioning.latestVersion
? `/${metadata.version}/`
: '/',
);
content = content.replace(
new RegExp(`\\]\\((\\./)?${mdLink}`, 'g'),
`](${htmlLink}`,
);
}
});
// replace markdown refernces to their website html links
new Set(mdReferences).forEach(refLink => {
let htmlLink = mdToHtml[refLink];
if (htmlLink) {
htmlLink = getPath(htmlLink, siteConfig.cleanUrl);
htmlLink = htmlLink.replace('/en/', `/${metadata.language}/`);
htmlLink = htmlLink.replace(
'/VERSION/',
metadata.version && metadata.version !== env.versioning.latestVersion
? `/${metadata.version}/`
: '/',
);
content = content.replace(
new RegExp(`\\]:(?:\\s)?(\\./|\\.\\./)?${refLink}`, 'g'),
`]: ${htmlLink}`,
);
}
});
return content;
}
function replaceAssetsLink(oldContent) {
let fencedBlock = false;
const lines = oldContent.split('\n').map(line => {
if (line.trim().startsWith('```')) {
fencedBlock = !fencedBlock;
}
return fencedBlock
? line
: line.replace(/\]\(assets\//g, `](${siteConfig.baseUrl}docs/assets/`);
});
return lines.join('\n');
}
function getMarkup(rawContent, mdToHtml, metadata) {
// generate table of contents
let content = insertTOC(rawContent);
// replace any links to markdown files to their website html links
content = mdToHtmlify(content, mdToHtml, metadata);
// replace any relative links to static assets (not in fenced code blocks) to absolute links
content = replaceAssetsLink(content);
const DocsLayout = require('../core/DocsLayout.js');
return renderToStaticMarkupWithDoctype(
<DocsLayout
metadata={metadata}
language={metadata.language}
config={siteConfig}>
{content}
</DocsLayout>,
);
}
function getRedirectMarkup(metadata) {
if (!env.translation.enabled || !metadata.permalink.includes('docs/en')) {
return null;
}
const Redirect = require('../core/Redirect.js');
const redirectlink = getPath(metadata.permalink, siteConfig.cleanUrl);
return renderToStaticMarkupWithDoctype(
<Redirect
metadata={metadata}
language={metadata.language}
config={siteConfig}
redirect={siteConfig.baseUrl + redirectlink}
/>,
);
}
module.exports = {
getMarkup,
getFile,
getFilePath,
getRedirectMarkup,
mdToHtmlify,
replaceAssetsLink,
};