Skip to content

Commit 44e0eb2

Browse files
committed
chore(md): mdn tweaks
1 parent 2db9702 commit 44e0eb2

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/md/rules/mdn.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ test('lowercases slug in rewritten URL', () => {
5050
expect(result?.pathname).toBe('/mdn/content/main/files/en-us/web/api/htmlelement/index.md')
5151
})
5252

53+
test('rewrites locale-less docs URL to mdn/content repo', () => {
54+
const rule = mdn()
55+
const url = new URL('https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch')
56+
const pattern = rule.patterns[1]
57+
assert(pattern instanceof URLPattern)
58+
const match = pattern.exec(url)!
59+
const result = rule.rewrite!(url, match)
60+
expect(result?.href).toBe(
61+
'https://raw.githubusercontent.com/mdn/content/main/files/en-us/web/api/fetch_api/using_fetch/index.md',
62+
)
63+
})
64+
5365
// Integration test
5466

5567
test('extract produces expected output for Array.prototype.map', async () => {
@@ -66,6 +78,24 @@ test('extract produces expected output for Array.prototype.map', async () => {
6678
expect(result.meta.title).toBe('Array.prototype.map()')
6779
})
6880

81+
test('locale-less docs URL uses markdown source so code fence info stays on the fence', async () => {
82+
const md = create({
83+
rules: [mdn()],
84+
fetch: async (input) => {
85+
const url = input instanceof URL ? input.href : typeof input === 'string' ? input : input.url
86+
expect(url).toBe(
87+
'https://raw.githubusercontent.com/mdn/content/main/files/en-us/web/api/fetch_api/using_fetch/index.md',
88+
)
89+
return new Response('---\ntitle: Test\n---\n\n```js\nconst x = 1\n```', { status: 200 })
90+
},
91+
})
92+
const result = await md.fetch('https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch')
93+
expect(result.ok).toBe(true)
94+
if (!result.ok) return
95+
expect(result.content).toContain('```js\nconst x = 1')
96+
expect(result.content).not.toContain('\njs\n\n```')
97+
})
98+
6999
// Extract behavior tests
70100

71101
test('converts jsxref macros to linked inline code', async () => {

src/md/rules/mdn.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const mdn = defineRule({
55
key: 'mdn',
66
patterns: [
77
new URLPattern({ hostname: 'developer.mozilla.org', pathname: '/:locale/docs/:path+' }),
8+
new URLPattern({ hostname: 'developer.mozilla.org', pathname: '/docs/:path+' }),
89
],
910
checks: [
1011
{
@@ -16,7 +17,7 @@ export const mdn = defineRule({
1617
},
1718
],
1819
rewrite(_url, match) {
19-
const locale = match.pathname.groups.locale?.toLowerCase()
20+
const locale = match.pathname.groups.locale?.toLowerCase() ?? 'en-us'
2021
const repo = locale === 'en-us' ? 'mdn/content' : 'mdn/translated-content'
2122
return new URL(
2223
`https://raw.githubusercontent.com/${repo}/main/files/${locale}/${match.pathname.groups.path!.toLowerCase()}/index.md`,

0 commit comments

Comments
 (0)