Skip to content

Commit b0c425d

Browse files
committedNov 24, 2023
feat: do not index unlisted content
closes #371
1 parent 683687d commit b0c425d

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed
 

‎.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
}
3636
],
3737
"rules": {
38+
"@typescript-eslint/no-explicit-any": "warn",
3839
"react-hooks/rules-of-hooks": "error",
3940
"react-hooks/exhaustive-deps": "warn"
4041
}

‎docusaurus-search-local/src/server/utils/parse.spec.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
import { parse } from "./parse";
66

77
describe("parse", () => {
8-
test.each<[string, "docs" | "blog" | "page", ParsedDocument]>([
8+
test.each<[string, "docs" | "blog" | "page", ParsedDocument | null]>([
99
[
1010
`<body>
1111
<article>
@@ -84,6 +84,24 @@ describe("parse", () => {
8484
breadcrumb: [],
8585
},
8686
],
87+
[
88+
`<head>
89+
<meta name="robots" content="noindex, nofollow" />
90+
</head>
91+
<body>
92+
<article>
93+
<header>
94+
<h1>Hello World</h1>
95+
</header>
96+
<main>
97+
<span class="ignore">Test</span>
98+
Peace.
99+
</main>
100+
</article>
101+
</body>`,
102+
"docs",
103+
null,
104+
],
87105
])("parse(...) should work", (html, type, doc) => {
88106
expect(
89107
parse(html, type, "", {

‎docusaurus-search-local/src/server/utils/parse.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ export function parse(
1111
type: "docs" | "blog" | "page",
1212
url: string,
1313
{ ignoreCssSelectors }: ProcessedPluginOptions
14-
): ParsedDocument {
14+
): ParsedDocument | null {
1515
const $ = cheerio.load(html);
16+
17+
const robotsMeta = $('meta[name="robots"]');
18+
if (robotsMeta.attr("content")?.includes("noindex")) {
19+
// Unlisted content
20+
return null;
21+
}
22+
1623
// Remove copy buttons from code boxes
1724
$('div[class^="mdxCodeBlock_"] button').remove();
1825

‎docusaurus-search-local/src/server/utils/scanDocuments.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ describe("scanDocuments", () => {
3333
url: "/2",
3434
type: "page",
3535
},
36+
{
37+
// Unlisted
38+
filePath: "/tmp/3",
39+
url: "/3",
40+
type: "docs",
41+
},
3642
];
3743
mockParse.mockImplementation((html) => {
3844
if (html.includes("1")) {
@@ -52,7 +58,7 @@ describe("scanDocuments", () => {
5258
],
5359
breadcrumb: ["Docs"],
5460
};
55-
} else {
61+
} else if (html.includes("2")) {
5662
return {
5763
pageTitle: "Hello First Page",
5864
sections: [
@@ -64,6 +70,8 @@ describe("scanDocuments", () => {
6470
],
6571
breadcrumb: [],
6672
};
73+
} else {
74+
return null;
6775
}
6876
});
6977
const allDocuments = await scanDocuments(

‎docusaurus-search-local/src/server/utils/scanDocuments.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ export async function scanDocuments(
3535
);
3636

3737
const html = await readFileAsync(filePath, { encoding: "utf8" });
38-
const { pageTitle, sections, breadcrumb } = parse(
39-
html,
40-
type,
41-
url,
42-
config
43-
);
38+
39+
const parsed = parse(html, type, url, config);
40+
if (!parsed) {
41+
// Unlisted content
42+
return;
43+
}
44+
const { pageTitle, sections, breadcrumb } = parsed;
4445

4546
const titleId = getNextDocId();
4647

‎website/docs/unlisted-post.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
unlisted: true
3+
---
4+
5+
# Unlisted Post
6+
7+
This unlisted blog post should be "hidden" in production, but remain accessible.
8+
9+
It is filtered from the sidebar, sitemap, SEO indexation...

0 commit comments

Comments
 (0)
Please sign in to comment.