Skip to content

Commit a498a5d

Browse files
committed
fix(cli): *index.md should not be treated as folder index files
1 parent eb6a23d commit a498a5d

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

.changeset/lemon-bulldogs-check.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket/cli': patch
3+
---
4+
5+
Make sure links to `*index.md` files are not treated as folder index files like `index.md`

packages/cli/src/eleventy-plugins/processLocalReferences.cjs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,25 @@ const templateEndings = [
3939
'.pug',
4040
];
4141

42-
function endsWithAny(string, suffixes) {
43-
for (let suffix of suffixes) {
44-
if (string.endsWith(suffix)) {
42+
function isTemplateFile(href) {
43+
for (const templateEnding of templateEndings) {
44+
if (href.endsWith(templateEnding)) {
4545
return true;
4646
}
4747
}
4848
return false;
4949
}
5050

51-
function isTemplateFile(href) {
52-
return endsWithAny(href, templateEndings);
53-
}
54-
5551
function isIndexTemplateFile(href) {
52+
const hrefParsed = path.parse(href);
5653
const indexTemplateEndings = templateEndings.map(ending => `index${ending}`);
57-
return endsWithAny(href, indexTemplateEndings);
54+
55+
for (const indexTemplateEnding of indexTemplateEndings) {
56+
if (hrefParsed.base === indexTemplateEnding) {
57+
return true;
58+
}
59+
}
60+
return false;
5861
}
5962

6063
/**
@@ -91,12 +94,46 @@ function extractReferences(html, inputPath) {
9194
return { hrefs, assets };
9295
}
9396

97+
/**
98+
* @param {string} inValue
99+
*/
100+
function getValueAndAnchor(inValue) {
101+
let value = inValue.replace(/&#/g, '--__check-html-links__--');
102+
let anchor = '';
103+
let suffix = '';
104+
105+
if (value.includes('#')) {
106+
[value, anchor] = value.split('#');
107+
suffix = `#${anchor}`;
108+
}
109+
if (value.includes('?')) {
110+
value = value.split('?')[0];
111+
}
112+
if (anchor.includes(':~:')) {
113+
anchor = anchor.split(':~:')[0];
114+
}
115+
if (value.includes(':~:')) {
116+
value = value.split(':~:')[0];
117+
}
118+
119+
value = value.replace(/--__check-html-links__--/g, '&#');
120+
anchor = anchor.replace(/--__check-html-links__--/g, '&#');
121+
suffix = suffix.replace(/--__check-html-links__--/g, '&#');
122+
value = value.trim();
123+
anchor = anchor.trim();
124+
125+
return {
126+
value,
127+
anchor,
128+
suffix,
129+
};
130+
}
131+
94132
function calculateNewHrefs(hrefs, inputPath) {
95133
const newHrefs = [];
96134
for (const hrefObj of hrefs) {
97135
const newHrefObj = { ...hrefObj };
98-
const [href, anchor] = newHrefObj.value.split('#');
99-
const suffix = anchor ? `#${anchor}` : '';
136+
const { value: href, suffix } = getValueAndAnchor(hrefObj.value);
100137

101138
if (isRelativeLink(href) && isTemplateFile(href)) {
102139
const hrefParsed = path.parse(href);

packages/cli/test-node/RocketCli.e2e.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ describe('RocketCli e2e', () => {
353353
expect(guidesHtml).to.equal('/_merged_assets/11ty-img/58b7e437-1200.png');
354354
});
355355

356-
it.only('will add "../" for links and image urls only within named template files', async () => {
356+
it('will add "../" for links and image urls only within named template files', async () => {
357357
await executeStart('e2e-fixtures/image-link/rocket.config.js');
358358

359359
const namedMdContent = [
@@ -415,13 +415,15 @@ describe('RocketCli e2e', () => {
415415
'<a href="guides/#with-anchor">Guides</a>',
416416
'<a href="./one-level/raw/">Raw</a>',
417417
'<a href="template/">Template</a>',
418+
'<a href="./rules/tabindex/">EndingIndex</a>',
418419
'<img src="./images/my-img.svg" alt="my-img">',
419420
'<img src="/images/my-img.svg" alt="absolute-img"></p>',
420421
'<div>',
421422
' <a href="./">Root</a>',
422423
' <a href="guides/#with-anchor">Guides</a>',
423424
' <a href="./one-level/raw/">Raw</a>',
424425
' <a href="template/">Template</a>',
426+
' <a href="./rules/tabindex/">EndingIndex</a>',
425427
' <img src="./images/my-img.svg" alt="my-img">',
426428
' <img src="/images/my-img.svg" alt="absolute-img">',
427429
' <picture>',

packages/cli/test-node/e2e-fixtures/image-link/docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[Guides](./guides.md#with-anchor)
33
[Raw](./one-level/raw.html)
44
[Template](./template.njk)
5+
[EndingIndex](./rules/tabindex.md)
56
![my-img](./images/my-img.svg)
67
![absolute-img](/images/my-img.svg)
78

@@ -10,6 +11,7 @@
1011
<a href="./guides.md#with-anchor">Guides</a>
1112
<a href="./one-level/raw.html">Raw</a>
1213
<a href="./template.njk">Template</a>
14+
<a href="./rules/tabindex.md">EndingIndex</a>
1315
<img src="./images/my-img.svg" alt="my-img">
1416
<img src="/images/my-img.svg" alt="absolute-img">
1517
<picture>

packages/cli/test-node/e2e-fixtures/image-link/docs/rules/tabindex.md

Whitespace-only changes.

0 commit comments

Comments
 (0)