Skip to content

Commit bbf62e3

Browse files
hectorsectorheiskr
andauthored
Remove redirects from markdown pages (#52355)
Co-authored-by: Kevin Heis <[email protected]>
1 parent 5fb07e2 commit bbf62e3

File tree

11 files changed

+47
-9
lines changed

11 files changed

+47
-9
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@
313313
"remark-parse": "^11.0.0",
314314
"remark-rehype": "^11.1.0",
315315
"remark-remove-comments": "^1.0.1",
316+
"remark-stringify": "^11.0.0",
316317
"rss-parser": "^3.13.0",
317318
"scroll-anchoring": "^0.1.0",
318319
"semver": "^7.6.2",

src/content-render/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderLiquid } from './liquid/index.js'
2-
import { renderUnified } from './unified/index.js'
2+
import { renderMarkdown, renderUnified } from './unified/index.js'
33
import { engine } from './liquid/engine.js'
44

55
const globalCache = new Map()
@@ -26,6 +26,12 @@ export async function renderContent(template = '', context = {}, options = {}) {
2626
}
2727
try {
2828
template = await renderLiquid(template, context)
29+
if (context.markdownRequested) {
30+
const md = await renderMarkdown(template, context, options)
31+
32+
return md
33+
}
34+
2935
const html = await renderUnified(template, context, options)
3036
if (cacheKey) {
3137
globalCache.set(cacheKey, html)

src/content-render/unified/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fastTextOnly } from './text-only.js'
2-
import { createProcessor } from './processor.js'
2+
import { createProcessor, createMarkdownOnlyProcessor } from './processor.js'
33

44
export async function renderUnified(template, context, options) {
55
const processor = createProcessor(context)
@@ -12,3 +12,11 @@ export async function renderUnified(template, context, options) {
1212

1313
return html.trim()
1414
}
15+
16+
export async function renderMarkdown(template, context, options) {
17+
const processor = createMarkdownOnlyProcessor(context)
18+
const vFile = await processor.process(template)
19+
let markdown = vFile.toString()
20+
21+
return markdown.trim()
22+
}

src/content-render/unified/processor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import annotate from './annotate.js'
2929
import alerts from './alerts.js'
3030
import replaceDomain from './replace-domain.js'
3131
import removeHtmlComments from 'remark-remove-comments'
32+
import remarkStringify from 'remark-stringify'
3233

3334
export function createProcessor(context) {
3435
return (
@@ -79,6 +80,10 @@ export function createProcessor(context) {
7980
)
8081
}
8182

83+
export function createMarkdownOnlyProcessor(context) {
84+
return unified().use(remarkParse).use(gfm).use(remarkStringify)
85+
}
86+
8287
export function createMinimalProcessor(context) {
8388
return unified()
8489
.use(remarkParse)

src/fixtures/tests/permissions-callout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('permission statements', () => {
3636
})
3737

3838
test('page with permission frontmatter and product statement', async () => {
39-
const $ = await getDOM('/get-started/foo/page-with-permissions-and-product-callout.md')
39+
const $ = await getDOM('/get-started/foo/page-with-permissions-and-product-callout')
4040
const html = $('[data-testid=permissions-callout] div').html()
4141
// part of the UI
4242
expect(html).toMatch('Who can use this feature')

src/frame/middleware/context/context.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ export default async function contextualize(
3838

3939
req.context.process = { env: {} }
4040

41+
if (req.pagePath && req.pagePath.endsWith('.md')) {
42+
req.context.markdownRequested = true
43+
44+
// req.pagePath is used later in the rendering pipeline to
45+
// locate the file in the tree so it cannot have .md
46+
req.pagePath = req.pagePath.replace(/\/index\.md$/, '').replace(/\.md$/, '')
47+
}
48+
4149
// define each context property explicitly for code-search friendliness
4250
// e.g. searches for "req.context.page" will include results from this file
4351
req.context.currentLanguage = req.language

src/frame/middleware/render-page.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,15 @@ export default async function renderPage(req: ExtendedRequest, res: Response) {
190190
}
191191
}
192192

193+
if (context.markdownRequested) {
194+
if (!page.autogenerated && page.documentType === 'article') {
195+
return res.type('text/markdown').send(req.context.renderedPage)
196+
} else {
197+
const newUrl = req.originalUrl.replace(req.path, req.path.replace(/\.md$/, ''))
198+
return res.redirect(newUrl)
199+
}
200+
}
201+
193202
defaultCacheControl(res)
194203

195204
return nextHandleRequest(req, res)

src/shielding/middleware/handle-invalid-paths.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,11 @@ export default function handleInvalidPaths(
8383
return res.status(404).send('Not found')
8484
}
8585

86-
if (req.path.endsWith('/index.md') || req.path.endsWith('.md')) {
86+
if (req.path.endsWith('/index.md')) {
8787
defaultCacheControl(res)
8888
// The originalUrl is the full URL including query string.
8989
// E.g. `/en/foo.md?bar=baz`
90-
const newUrl = req.originalUrl.replace(
91-
req.path,
92-
req.path.replace(/\/index\.md$/, '').replace(/\.md$/, ''),
93-
)
90+
const newUrl = req.originalUrl.replace(req.path, req.path.replace(/\/index\.md$/, ''))
9491
return res.redirect(newUrl)
9592
}
9693

src/shielding/tests/shielding.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ describe('index.md and .md suffixes', () => {
7171
}
7272
})
7373

74-
test('any URL that ends with /.md redirects', async () => {
74+
// TODO-ARTICLEAPI: unskip tests or replace when ready to ship article API
75+
test.skip('any URL that ends with /.md redirects', async () => {
7576
// With language prefix
7677
{
7778
const res = await get('/en/get-started/hello.md')

0 commit comments

Comments
 (0)