Skip to content

Commit 554c451

Browse files
committed
fix: refine debug
1 parent 3b1470b commit 554c451

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ html[data-theme="dark"] {
115115
}
116116
```
117117

118+
## Trouble Shooting
119+
120+
When building your docs project, Set the env `DEBUG=search-local:*` to enable [debug](https://github.com/visionmedia/debug) logs.
121+
122+
```shell
123+
# In your docs project:
124+
DEBUG=search-local:* yarn build
125+
```
126+
118127
## Contributing
119128

120129
See [contributing guide](CONTRIBUTING.md).

src/server/utils/debug.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import createDebug from "debug";
2+
3+
const debug = createDebug("search-local");
4+
export const debugVerbose = debug.extend("verbose");
5+
export const debugInfo = debug.extend("info");
6+
export const debugWarn = debug.extend("warn");

src/server/utils/parsePage.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { ParsedDocument } from "../../shared/interfaces";
2+
import { debugWarn } from "./debug";
23
import { getCondensedText } from "./getCondensedText";
3-
import _debug from "debug";
4-
5-
const debug = _debug("search-local");
64

75
export function parsePage($: cheerio.Root, url: string): ParsedDocument {
86
$("a[aria-hidden=true]").remove();
97

10-
$("a[aria-hidden=true]").remove();
118
let $pageTitle = $("h1").first();
129
if ($pageTitle.length === 0) {
1310
$pageTitle = $("title");
@@ -17,8 +14,8 @@ export function parsePage($: cheerio.Root, url: string): ParsedDocument {
1714

1815
const $main = $("main");
1916
if ($main.length === 0) {
20-
debug(
21-
"Page has no <main>, therefore no content was indexed for this page '%s'.",
17+
debugWarn(
18+
"page has no <main>, therefore no content was indexed for this page %o",
2219
url
2320
);
2421
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
import fs from "fs";
22
import path from "path";
33
import util from "util";
4-
import _debug from "debug";
54
import { ProcessedPluginOptions, PostBuildData } from "../../shared/interfaces";
65
import { buildIndex } from "./buildIndex";
6+
import { debugInfo } from "./debug";
77
import { processDocInfos } from "./processDocInfos";
88
import { scanDocuments } from "./scanDocuments";
99

10-
const debug = _debug("search-local");
1110
const writeFileAsync = util.promisify(fs.writeFile);
1211

1312
export function postBuildFactory(config: ProcessedPluginOptions) {
1413
return async function postBuild(buildData: PostBuildData): Promise<void> {
15-
debug("Gathering documents");
14+
debugInfo("gathering documents");
1615

1716
const data = processDocInfos(buildData, config);
1817

19-
debug("Parsing documents");
18+
debugInfo("parsing documents");
2019

2120
// Give every index entry a unique id so that the index does not need to store long URLs.
2221
const allDocuments = await scanDocuments(data);
2322

24-
debug("Building index");
23+
debugInfo("building index");
2524

2625
const searchIndex = buildIndex(allDocuments, config);
2726

28-
debug("Writing index to disk");
27+
debugInfo("writing index to disk");
2928

3029
await writeFileAsync(
3130
path.join(buildData.outDir, "search-index.json"),
3231
JSON.stringify(searchIndex),
3332
{ encoding: "utf8" }
3433
);
3534

36-
debug("Index written to disk, success!");
35+
debugInfo("index written to disk successfully!");
3736
};
3837
}

src/server/utils/scanDocuments.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import fs from "fs";
2+
import path from "path";
23
import util from "util";
3-
import _debug from "debug";
44
import { DocInfoWithFilePath, SearchDocument } from "../../shared/interfaces";
55
import { parse } from "./parse";
6+
import { debugVerbose } from "./debug";
67

78
const readFileAsync = util.promisify(fs.readFile);
8-
const debug = _debug("search-local");
99

1010
let nextDocId = 0;
1111
const getNextDocId = () => {
@@ -22,7 +22,12 @@ export async function scanDocuments(
2222

2323
await Promise.all(
2424
DocInfoWithFilePathList.map(async ({ filePath, url, type }) => {
25-
debug(`Parsing ${type} file ${filePath}`, { url });
25+
debugVerbose(
26+
`parsing %s file %o of %o`,
27+
type,
28+
path.relative(process.cwd(), filePath),
29+
url
30+
);
2631

2732
const html = await readFileAsync(filePath, { encoding: "utf8" });
2833
const { pageTitle, sections } = parse(html, type, url);

0 commit comments

Comments
 (0)