fix: markdown linking for translated & versioned docs

This commit is contained in:
endiliey 2018-09-14 23:19:47 +08:00
parent e9f2fabde1
commit c0194a1f53
11 changed files with 306 additions and 19 deletions

View file

@ -8,25 +8,36 @@ module.exports = function(fileString) {
versionedDir,
docsDir,
translatedDir,
sourceToLink
sourceToMetadata
} = options;
/* Extract content of markdown (without frontmatter) */
const {body} = fm(fileString);
/* Determine whether this file is in @docs, @versioned_docs or @translated_docs */
let dirAlias;
if (this.resourcePath.startsWith(translatedDir)) {
dirAlias = '@translated_docs';
} else if (this.resourcePath.startsWith(versionedDir)) {
dirAlias = '@versioned_docs';
} else if (this.resourcePath.startsWith(docsDir)) {
dirAlias = '@docs';
/* Determine the source dir. e.g: @docs, @translated_docs/ko and @versioned_docs/version-1.0.0 */
let sourceDir;
let thisSource = this.resourcePath;
if (thisSource.startsWith(translatedDir)) {
thisSource = thisSource.replace(translatedDir, '@translated_docs');
const {language, version} = sourceToMetadata[thisSource] || {};
if (language && version && version !== 'next') {
sourceDir = `@translated_docs/${language}/version-${version}`;
} else if (language && (!version || version === 'next')) {
sourceDir = `@translated_docs/${language}`;
}
} else if (thisSource.startsWith(versionedDir)) {
thisSource = thisSource.replace(versionedDir, '@versioned_docs');
const {version} = sourceToMetadata[thisSource] || {};
if (version) {
sourceDir = `@versioned_docs/version-${version}`;
}
} else if (thisSource.startsWith(docsDir)) {
sourceDir = `@docs`;
}
/* Replace internal markdown linking (except in fenced blocks) */
let content = body;
if (dirAlias) {
if (sourceDir) {
let fencedBlock = false;
const lines = body.split('\n').map(line => {
if (line.trim().startsWith('```')) {
@ -43,8 +54,8 @@ module.exports = function(fileString) {
match = mdRegex.exec(content);
}
mdLinks.forEach(mdLink => {
const source = `${dirAlias}/${mdLink}`;
const permalink = sourceToLink[source];
const targetSource = `${sourceDir}/${mdLink}`;
const {permalink} = sourceToMetadata[targetSource] || {};
if (permalink) {
modifiedLine = modifiedLine.replace(mdLink, permalink);
}