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

@ -27,10 +27,16 @@ module.exports = async function load(siteDir) {
); );
/* Create source to permalink mapping */ /* Create source to permalink mapping */
const sourceToLink = {}; const sourceToMetadata = {};
Object.values(docsMetadata).forEach(({source, permalink}) => { Object.values(docsMetadata).forEach(
sourceToLink[source] = permalink; ({source, version, permalink, language}) => {
}); sourceToMetadata[source] = {
version,
permalink,
language
};
}
);
// pages // pages
const pagesDir = path.resolve(siteDir, 'pages'); const pagesDir = path.resolve(siteDir, 'pages');
@ -61,7 +67,7 @@ module.exports = async function load(siteDir) {
outDir, outDir,
themePath, themePath,
baseUrl, baseUrl,
sourceToLink, sourceToMetadata,
versionedDir, versionedDir,
translatedDir translatedDir
}; };

View file

@ -13,7 +13,7 @@ module.exports = function createBaseConfig(props, isServer) {
docsDir, docsDir,
pagesDir, pagesDir,
siteDir, siteDir,
sourceToLink, sourceToMetadata,
versionedDir, versionedDir,
translatedDir, translatedDir,
baseUrl baseUrl
@ -78,7 +78,13 @@ module.exports = function createBaseConfig(props, isServer) {
mdRule mdRule
.use('markdown-loader') .use('markdown-loader')
.loader(mdLoader) .loader(mdLoader)
.options({siteConfig, versionedDir, translatedDir, docsDir, sourceToLink}); .options({
siteConfig,
versionedDir,
translatedDir,
docsDir,
sourceToMetadata
});
const cssRule = config.module.rule('css').test(/\.css$/); const cssRule = config.module.rule('css').test(/\.css$/);
if (isProd) { if (isProd) {

View file

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

View file

@ -5,6 +5,20 @@ title: Hello, World !
Hi, Endilie here :) Hi, Endilie here :)
## Relative links
Replace this
[foo](foo/bar.md)
Can't replace this
[file](file.md)
Do not replace below
```
[hello](hello.md)
```
## Blockquotes ## Blockquotes
> Blockquotes can also be nested... > Blockquotes can also be nested...

View file

@ -0,0 +1,66 @@
---
id: bar
title: Bar
---
# Remarkable
> Remarkable로 실시간 편집을 경험하십시오!
깨끗한 슬레이트로 시작하려면`clear` 링크를 클릭하고 결과를 공유하거나 저장하려면`permalink`를 가져옵니다.
***
# h1 제목
## h2 제목
### h3 제목
#### h4 헤딩
##### h5 제목
###### h6 제목
## 수평 규칙
___
***
***
## 활자체 대체
입력기 옵션을 사용하면 결과를 볼 수 있습니다.
(p) (P) + - (r) (t)
테스트 .. 테스트 ... 테스트 ..... 테스트? ..... 테스트! ....
!!!!!! ???? ,,
놀라운 - 굉장한
"Smartypants, 큰 따옴표"
'Smartypants, 작은 따옴표'
## 강조
** 이것은 굵은 글씨입니다 **
__ 이것은 굵은 글씨입니다 __
* 이탤릭체 텍스트 *
_ 이탤릭체 텍스트 _
~~ 삭제 된 텍스트 ~~
위 첨자 : 19 ^ th ^
아래 첨자 : H ~ 2 ~ O
++ 삽입 된 텍스트 ++
== 표시된 텍스트 ==

View file

@ -0,0 +1,74 @@
---
id: baz
title: baz
---
## 이미지
링크와 마찬가지로 이미지에도 각주 스타일 구문이 있습니다.
! [Alt text] [id]
나중에 URL 위치를 정의하는 문서에서 참조로 :
[id] : https://octodex.github.com/images/dojocat.jpg "The Dojocat"
## 링크
[링크 텍스트] (http://dev.nodeca.com)
[제목 링크] (http://nodeca.github.io/pica/demo/ "제목 텍스트!")
자동 변환 된 링크 https://github.com/nodeca/pica (linkify를 사용하도록 설정)
## 각주
각주 1 링크 [^ 첫 번째].
각주 2 링크 [^ 초].
인라인 각주 ^ [인라인 각주의 텍스트] 정의.
중복 된 각주 참조 [^ 초].
[^ first] : 각주 **는 마크 업을 가질 수 있습니다 **
    및 여러 단락.
[^ 초] : 각주 텍스트.
## 정의 목록
1 학기
정의 1
게으른 연속.
* 인라인 마크 업과 함께 2 학기 *
: 정의 2
        {일부 코드, 정의 2의 일부}
    정의의 세 번째 단락 2.
_ 컴팩트 스타일 : _
1 학기
  ~ 정의 1
2 학기
  ~ 정의 2a
  ~ 정의 2b
## 약어
이것은 HTML 약어입니다.
그것은 "HTML"을 변환하지만 "xxxHTMLyyy"와 같이 부분적인 항목을 그대로 유지합니다.
* [HTML] : 하이퍼 텍스트 마크 업 언어

View file

@ -0,0 +1,54 @@
---
id: hello
title: Hello, World !
---
안녕하세요, 여기 엔 틸리에 :)
## 상대 링크
이것 바꾸기
[foo](foo/bar.md)
이것을 대체 할 수 없습니다.
[파일] (file.md)
아래를 교체하지 마십시오.
```
[hello] (hello.md)
```
## Blockquotes
> Blockquotes는 또한 중첩 될 수 있습니다 ...
>> ... 서로 옆에 큰 더 큰 부호를 사용하여 ...
>>> ... 또는 화살표 사이에 공백이 있어야합니다.
## 목록
정렬되지 않은
+`+`,`-` 또는`*`를 사용하여 행을 시작하여 목록을 만듭니다.
+ 하위 목록은 2 칸을 들여서 만들어집니다 :
   - 마커 문자 변경으로 새로운 목록 시작 :
     * AC tristique libero volutpat at
     + Preisium nisl aliquet에 대한 + 시설
     - Nulla volutpat aliquam velit
+ 매우 쉽습니다!
주문 됨
1. Lorem ipsum dolor sit amet
2. 컨소시엄 adipiscing 엘리트
3. massa에서의 정수 lorem
1. 일련 번호를 사용할 수 있습니다 ...
1. ... 또는 모든 숫자를 '1'로 유지하십시오.
오프셋을 사용하여 번호 매기기 시작 :
57. foo
1. 막대기

View file

@ -5,6 +5,20 @@ title: Hello, World !
안녕하세요, 여기 엔 틸리에 :) 안녕하세요, 여기 엔 틸리에 :)
## 상대 링크
이것 바꾸기
[foo](foo/bar.md)
이것을 대체 할 수 없습니다.
[파일] (file.md)
아래를 교체하지 마십시오.
```
[hello] (hello.md)
```
## Blockquotes ## Blockquotes
> Blockquotes는 또한 중첩 될 수 있습니다 ... > Blockquotes는 또한 중첩 될 수 있습니다 ...

View file

@ -5,6 +5,20 @@ title: Hello, World !
안녕하세요, 여기 엔 틸리에 :) 안녕하세요, 여기 엔 틸리에 :)
## 상대 링크
이것 바꾸기
[foo](foo/bar.md)
이것을 대체 할 수 없습니다.
[파일] (file.md)
아래를 교체하지 마십시오.
```
[hello] (hello.md)
```
## Blockquotes ## Blockquotes
> Blockquotes는 또한 중첩 될 수 있습니다 ... > Blockquotes는 또한 중첩 될 수 있습니다 ...

View file

@ -5,6 +5,20 @@ title: Hello, World !
Hi, Endilie here :) Hi, Endilie here :)
## Relative links
Replace this
[foo](foo/bar.md)
Can't replace this
[file](file.md)
Do not replace below
```
[hello](hello.md)
```
## Blockquotes ## Blockquotes
> Blockquotes can also be nested... > Blockquotes can also be nested...

View file

@ -5,6 +5,20 @@ title: Hello, World !
Hi, Endilie here :) Hi, Endilie here :)
## Relative links
Replace this
[foo](foo/bar.md)
Can't replace this
[file](file.md)
Do not replace below
```
[hello](hello.md)
```
## Blockquotes ## Blockquotes
> Blockquotes can also be nested... > Blockquotes can also be nested...