Skip to content

Commit c569acf

Browse files
committed
Handle more cases, update tests
1 parent a3609b0 commit c569acf

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/content/tutorials/en/p5js-with-screen-reader.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,4 @@ white circle
234234
235235
The p5.js Web Editor has several features that make it easier to code while using a screen reader. Warning sounds can alert you to syntax errors while you code. Text descriptions of the canvas can help you to keep track of visual outputs. 
236236
237-
Keep in mind that the features described above only work within the p5.js Web Editor while you code. Check out [How to Label Your p5.js Canvas](https://p5js.org/learn/labeling-canvases.html) for detailed guidance on p5.js code accessibility. This is an important step for making your project accessible when sharing it outside of the p5.js Web Editor.
237+
Keep in mind that the features described above only work within the p5.js Web Editor while you code. Check out [How to Label Your p5.js Canvas](./writing-accessible-canvas-descriptions) for detailed guidance on p5.js code accessibility. This is an important step for making your project accessible when sharing it outside of the p5.js Web Editor.

src/pages/_utils.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ const getUrl = (
432432
export const rewriteRelativeLink = (url: string): string => {
433433
let updatedUrl: string;
434434

435-
if (/^(https?:\/)?\//.exec(url) || url.startsWith('mailto:')) {
435+
if (/^((https?:\/)?)\//.exec(url) || url.startsWith('mailto:')) {
436436
// Leave absolute paths alone
437437
updatedUrl = url;
438438
} else if (url.startsWith('#')) {
@@ -448,14 +448,19 @@ export const rewriteRelativeLink = (url: string): string => {
448448
} else {
449449
updatedUrl = url;
450450
}
451+
452+
// Relative links to md files should be turned into pages
453+
if (updatedUrl.endsWith('.md')) {
454+
updatedUrl = updatedUrl.replace(/\.md$/, '');
455+
}
451456
}
452457

453458
// Add a trailing / if the link isn't to a file and does not have query params or a hash reference
454459
if (
455-
!url.endsWith('/') &&
456-
!/(\.\w+)$/.exec(url) &&
457-
!url.includes('?') &&
458-
!/#([\w\-]+)$/.exec(url)
460+
!updatedUrl.endsWith('/') &&
461+
!/(\.\w+)$/.exec(updatedUrl) &&
462+
!updatedUrl.includes('?') &&
463+
!/#([\w\-]+)$/.exec(updatedUrl)
459464
) {
460465
updatedUrl += '/';
461466
}

src/scripts/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ export const rewriteRelativeMdLinks = (markdownText: string): string => {
277277
* 1. Text for the link
278278
* 2. Link url (but not the .md extension at the end)
279279
*/
280-
const regexPattern: RegExp = /\[([^\]]+)\]\((.?\/?[^)]+)\.md\)/g;
281-
return markdownText.replace(regexPattern, (_match, linkText, url: string) => {
280+
const regexPattern: RegExp = /(\!?)\[([^\]]+)\]\(([^\)]+)\)/g;
281+
return markdownText.replace(regexPattern, (match, img, linkText, url: string) => {
282+
// Don't convert images
283+
if (img) return match;
284+
282285
const updatedUrl = rewriteRelativeLink(url);
283286
return `[${linkText}](${updatedUrl})`;
284287
});

test/scripts/utils.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@ test("rewriteRelativeMdLinks", () => {
1616
- [Other](./folder/document.md)
1717
- [Other](folder/document.md)
1818
- [lol](./some-where-else/wow/)
19+
- [absolute](/wow/)
20+
- [no trailing slash](../test)
21+
- [external](https://p5js.org/)
1922
`),
2023
).toEqual(`
2124
# Title
2225
2326
You can find more examples [here](https://p5xjs.org/examples.html) and
24-
in the [guide](./the-guide/).
27+
in the [guide](../the-guide/).
2528
2629
![image](./assets/image.jpg)
2730
2831
## Related Docs
29-
- See [Access](./access/).
30-
- [Other](./folder/document/)
31-
- [Other](folder/document/)
32-
- [lol](./some-where-else/wow/)
32+
- See [Access](../access/).
33+
- [Other](../folder/document/)
34+
- [Other](../folder/document/)
35+
- [lol](../some-where-else/wow/)
36+
- [absolute](/wow/)
37+
- [no trailing slash](../test/)
38+
- [external](https://p5js.org/)
3339
`);
3440
});

0 commit comments

Comments
 (0)