Skip to content

Commit 4de9b6d

Browse files
committed
Add test for replacing reference links within a doc
1 parent 137593d commit 4de9b6d

5 files changed

Lines changed: 122 additions & 0 deletions

File tree

v1/lib/server/__tests__/__fixtures__/metadata.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ module.exports = {
5151
previous_title: 'Document 2',
5252
sort: 3,
5353
},
54+
'en-reflinks': {
55+
id: 'en-reflinks',
56+
title: 'Reference Links',
57+
source: 'reflinks.md',
58+
version: 'next',
59+
permalink: 'docs/en/next/reflinks.html',
60+
localized_id: 'reflinks',
61+
language: 'en',
62+
sidebar: 'docs',
63+
category: 'Test 2',
64+
previous_id: 'doc3',
65+
previous: 'en-doc3',
66+
previous_title: 'Document 3',
67+
sort: 4,
68+
},
5469
'ko-doc1': {
5570
id: 'ko-doc1',
5671
title: '문서 1',
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
id: reflinks
3+
title: Reference Links
4+
---
5+
6+
### Existing Docs
7+
8+
- [doc1][doc1]
9+
- [doc2][doc2]
10+
11+
### Non-existing Docs
12+
13+
- [hahaha][hahaha]
14+
15+
## Repeating Docs
16+
17+
- [doc1][doc1]
18+
- [doc2][doc2]
19+
20+
## Do not replace this
21+
```md
22+
![image1][image1]
23+
```
24+
25+
```js
26+
const doc1 = foo();
27+
console.log("[image2][image2]");
28+
const testStr = `![image3][image3]`;
29+
```
30+
31+
[doc1]: doc1.md
32+
[doc2]: ./doc2.md
33+
[hahaha]: hahaha.md
34+
[image1]: assets/image1.png
35+
[image2]: assets/image2.jpg
36+
[image3]: assets/image3.gif

v1/lib/server/__tests__/__snapshots__/docs.test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,42 @@ const testStr = \`![image3](assets/image3.gif)\`;
5757
\`\`\`"
5858
`;
5959

60+
exports[`mdToHtmlify transforms reference links 1`] = `
61+
"
62+
### Existing Docs
63+
64+
- [doc1][doc1]
65+
- [doc2][doc2]
66+
67+
### Non-existing Docs
68+
69+
- [hahaha][hahaha]
70+
71+
## Repeating Docs
72+
73+
- [doc1][doc1]
74+
- [doc2][doc2]
75+
76+
## Do not replace this
77+
\`\`\`md
78+
![image1][image1]
79+
\`\`\`
80+
81+
\`\`\`js
82+
const doc1 = foo();
83+
console.log(\\"[image2][image2]\\");
84+
const testStr = \`![image3][image3]\`;
85+
\`\`\`
86+
87+
[doc1]: /docs/en/next/doc1
88+
[doc2]: /docs/en/next/doc2
89+
[hahaha]: hahaha.md
90+
[image1]: assets/image1.png
91+
[image2]: assets/image2.jpg
92+
[image3]: assets/image3.gif
93+
"
94+
`;
95+
6096
exports[`replaceAssetsLink does not transform document without valid assets link 1`] = `
6197
"
6298
### Existing Docs

v1/lib/server/__tests__/__snapshots__/readCategories.test.js.snap

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ Array [
6464
},
6565
"type": "LINK",
6666
},
67+
Object {
68+
"item": Object {
69+
"category": "Test 2",
70+
"id": "en-reflinks",
71+
"language": "en",
72+
"localized_id": "reflinks",
73+
"permalink": "docs/en/next/reflinks.html",
74+
"previous": "en-doc3",
75+
"previous_id": "doc3",
76+
"previous_title": "Document 3",
77+
"sidebar": "docs",
78+
"sort": 4,
79+
"source": "reflinks.md",
80+
"title": "Reference Links",
81+
"version": "next",
82+
},
83+
"type": "LINK",
84+
},
6785
],
6886
"title": "Test 2",
6987
"type": "CATEGORY",

v1/lib/server/__tests__/docs.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,15 @@ const doc3 = fs.readFileSync(
5555
'utf8',
5656
);
5757

58+
const refLinks = fs.readFileSync(
59+
path.join(__dirname, '__fixtures__', 'reflinks.md'),
60+
'utf8',
61+
);
62+
5863
const rawContent1 = metadataUtils.extractMetadata(doc1).rawContent;
5964
const rawContent2 = metadataUtils.extractMetadata(doc2).rawContent;
6065
const rawContent3 = metadataUtils.extractMetadata(doc3).rawContent;
66+
const rawContentRefLinks = metadataUtils.extractMetadata(refLinks).rawContent;
6167

6268
describe('mdToHtmlify', () => {
6369
const mdToHtml = metadataUtils.mdToHtml(Metadata, '/');
@@ -105,6 +111,17 @@ describe('mdToHtmlify', () => {
105111
expect(content3).toMatchSnapshot();
106112
expect(content3).not.toEqual(rawContent3);
107113
});
114+
115+
test('transforms reference links', () => {
116+
const contentRefLinks = docs.mdToHtmlify(
117+
rawContentRefLinks,
118+
mdToHtml,
119+
Metadata['en-reflinks'],
120+
);
121+
expect(contentRefLinks).toContain('/docs/en/next/');
122+
expect(contentRefLinks).toMatchSnapshot();
123+
expect(contentRefLinks).not.toEqual(rawContentRefLinks);
124+
});
108125
});
109126

110127
describe('getFile', () => {

0 commit comments

Comments
 (0)