Skip to content

Commit d7b52e7

Browse files
peterbesarahs
andauthored
catch typos near 'AUTOTITLE' (github#34493)
Co-authored-by: Sarah Schneider <[email protected]>
1 parent f99da59 commit d7b52e7

File tree

8 files changed

+88
-6
lines changed

8 files changed

+88
-6
lines changed

lib/render-content/plugins/rewrite-local-links.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path'
22
import { visit } from 'unist-util-visit'
3+
import { distance } from 'fastest-levenshtein'
34
import { getPathWithoutLanguage, getVersionStringFromPath } from '../../path-utils.js'
45
import { getNewVersionedPath } from '../../old-versions-utils.js'
56
import patterns from '../../patterns.js'
@@ -40,8 +41,21 @@ export default function rewriteLocalLinks(context) {
4041
node.properties.href = newHref
4142
}
4243
for (const child of node.children) {
43-
if (child.value && AUTOTITLE.test(child.value)) {
44-
promises.push(getNewTitleSetter(child, node.properties.href, context))
44+
if (child.value) {
45+
if (AUTOTITLE.test(child.value)) {
46+
promises.push(getNewTitleSetter(child, node.properties.href, context))
47+
} else if (process.env.NODE_ENV !== 'production') {
48+
// Throw if the link text *almost* is AUTOTITLE
49+
if (
50+
child.value.toUpperCase() === 'AUTOTITLE' ||
51+
distance(child.value.toUpperCase(), 'AUTOTITLE') <= 2
52+
) {
53+
throw new Error(
54+
`Found link text '${child.value}', expected 'AUTOTITLE'. ` +
55+
`Find the mention of the link text '${child.value}' and change it to 'AUTOTITLE'. Case matters.`
56+
)
57+
}
58+
}
4559
}
4660
}
4761
})

middleware/handle-errors.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { nextApp } from './next.js'
33
import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js'
44
import { errorCacheControl } from './cache-control.js'
55

6+
const DEBUG_MIDDLEWARE_TESTS = Boolean(JSON.parse(process.env.DEBUG_MIDDLEWARE_TESTS || 'false'))
7+
68
function shouldLogException(error) {
79
const IGNORED_ERRORS = [
810
// Client connected aborted
@@ -43,7 +45,7 @@ export default async function handleError(error, req, res, next) {
4345
// loading all the middlewares.
4446
setFastlySurrogateKey(res, SURROGATE_ENUMS.DEFAULT)
4547
}
46-
} else if (process.env.NODE_ENV === 'test') {
48+
} else if (DEBUG_MIDDLEWARE_TESTS) {
4749
console.warn('An error occurrred in some middleware handler', error)
4850
}
4951

package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dotenv": "^16.0.1",
3131
"express": "^4.18.1",
3232
"express-timeout-handler": "^2.2.2",
33+
"fastest-levenshtein": "1.0.16",
3334
"file-type": "18.2.0",
3435
"flat": "^5.0.2",
3536
"github-slugger": "^2.0.0",

tests/README.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ but it's helpful to run tests locally before pushing your changes to
66
GitHub.
77

88
Tests are written using [jest](https://ghub.io/jest), a framework maintained
9-
by Facebook and used by many teams at GitHub.
9+
by Facebook and used by many teams at GitHub.
1010
Jest provides everything: a test runner, an assertion library, code coverage analysis,
1111
custom reporters for different types of test output, etc.
1212

@@ -98,7 +98,21 @@ In another terminal, type:
9898
START_JEST_SERVER=false jest tests/rendering/foo/bar.js
9999
```
100100

101-
Or whatever the testing command you use is.
101+
Or whatever the testing command you use is.
102102

103103
The `START_JEST_SERVER` environment variable needs to be set to `false`, or else `jest` will try to start
104104
a server on `:4000` too.
105+
106+
### Debugging middleware errors
107+
108+
By default, errors handled by the middleware are dealt with just like
109+
any error in production. It's common to have end-to-end tests that expect
110+
a page to throw a 500 Internal Server Error response.
111+
112+
If you don't expect that and you might struggle to see exactly where the
113+
error is happening, set `$DEBUG_MIDDLEWARE_TESTS` to `true`. For example:
114+
115+
```sh
116+
export DEBUG_MIDDLEWARE_TESTS=true
117+
jest tests/rendering/ -b
118+
```

tests/fixtures/content/get-started/foo/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ versions:
1010
children:
1111
- /bar
1212
- /autotitling
13+
- /typo-autotitling
1314
- /cross-version-linking
1415
---
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Typo autotitling
3+
intro: If the author typos the word "AUTOTITLE" it will throw
4+
versions:
5+
fpt: '*'
6+
ghes: '*'
7+
ghae: '*'
8+
type: how_to
9+
---
10+
11+
## Example
12+
13+
{% ifversion ghes %}
14+
15+
"[Autotitle](/get-started/quickstart/hello-world)."
16+
17+
{% else %}
18+
19+
"[AUTOTITLES](/get-started/quickstart/hello-world)."
20+
21+
{% endif %}

tests/rendering-fixtures/internal-links.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDOM } from '../helpers/e2etest.js'
1+
import { get, getDOM } from '../helpers/e2etest.js'
22
import enterpriseServerReleases from '../../lib/enterprise-server-releases.js'
33
import { allVersions } from '../../lib/all-versions.js'
44

@@ -14,6 +14,21 @@ describe('autotitle', () => {
1414
// There are 4 links on the `autotitling.md` content.
1515
expect.assertions(4)
1616
})
17+
18+
test('typos lead to error when NODE_ENV !== production', async () => {
19+
// The fixture typo-autotitling.md contains two different typos
20+
// of the word "AUTOTITLE", separated by `{% if version ghes %}`
21+
{
22+
const res = await get('/get-started/foo/typo-autotitling', { followRedirects: true })
23+
expect(res.statusCode).toBe(500)
24+
}
25+
{
26+
const res = await get('/enterprise-server@latest/get-started/foo/typo-autotitling', {
27+
followRedirects: true,
28+
})
29+
expect(res.statusCode).toBe(500)
30+
}
31+
})
1732
})
1833

1934
describe('cross-version-links', () => {

0 commit comments

Comments
 (0)