Skip to content

Commit 093201c

Browse files
committed
fix(lc): fix the process to handle the question has no enough data
fix #60
1 parent 1c00952 commit 093201c

File tree

7 files changed

+16
-12
lines changed

7 files changed

+16
-12
lines changed

common/utils/file/getLineNumberByContent.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs';
2+
import { logger } from '#common/utils/logger/logger.js';
23

34
/**
45
* 通过给入的文件地址和内容 给出对应的行号
@@ -24,12 +25,12 @@ export function getLineNumberByContent(filePath, searchString) {
2425
});
2526

2627
readStream.on('end', () => {
27-
console.warn(`"${searchString}" not found in file: ${filePath}`);
28+
logger.warn(`[WARN] "${searchString}" not found in file: ${filePath}`);
2829
resolve(0);
2930
});
3031

3132
readStream.on('error', () => {
32-
console.warn(`"${searchString}" not found in file: ${filePath}`);
33+
logger.warn(`[WARN] "${searchString}" not found in file: ${filePath}`);
3334
resolve(0);
3435
});
3536
});

common/utils/functions/removeDomTags.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
export function removeDomTags(input) {
77
return input
8-
.replace(/<[^>]*>/g, '')
8+
?.replace(/<[^>]*>/g, '')
99
.replaceAll(' ', ' ')
1010
.replaceAll('&nbsp;', ' ')
1111
.replaceAll('&lt;', '<')

common/utils/question-handler/code.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export async function getCodeBySlug(slug, lang) {
1919
* @returns {Promise<string[]>}
2020
*/
2121
export async function getSupportCode(slug) {
22-
const list = await getQuestionCodeList(slug);
23-
return list.map((code) => code?.langSlug);
22+
const list = (await getQuestionCodeList(slug)) ?? [];
23+
return list?.map((code) => code?.langSlug);
2424
}
2525

2626
/**

common/utils/question-handler/createMarkdown.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import path from 'node:path';
77
* @param {*} questionPath
88
*/
99
export function createMarkdown(description, questionPath) {
10+
if (!description) return;
1011
const dir = path.dirname(questionPath);
1112
const descriptionPath = path.join(dir, 'description.md');
1213
fs.writeFileSync(descriptionPath, description);

common/utils/question-handler/fulfillQuestion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { getCodeRange } from '#common/utils/question-handler/code.js';
2929
*/
3030
export function generateTemplateContent(question) {
3131
const title = `${getQuestionChineseName(question)} ${question.date ? `[${question.date}]` : ''}\n`;
32-
const describe = removeDomTags(question.detail).replace(/\n+/g, '\n');
32+
const describe = removeDomTags(question.detail)?.replace(/\n+/g, '\n') ?? '';
3333
const lang = question.lang;
3434
const code = question.code;
3535
return template

common/utils/question-handler/getTestCase.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export function getTestCase(question) {
5252
// 结尾
5353
const endReg = /(<\/[a-zA-Z]+>)?/gm;
5454

55-
const detail = question.detail.replaceAll('`', '');
56-
const cases = detail.match(inputReg)?.map(
55+
const detail = question.detail?.replaceAll('`', '');
56+
const cases = detail?.match(inputReg)?.map(
5757
(str) =>
5858
`[${removeDomTags(
5959
str
@@ -63,7 +63,7 @@ export function getTestCase(question) {
6363
.replace(/[a-zA-Z]+ =/g, '')
6464
)}]`
6565
);
66-
const expires = detail.match(outputReg)?.map((str) =>
66+
const expires = detail?.match(outputReg)?.map((str) =>
6767
removeDomTags(
6868
str
6969
?.replace(outputStartReg, '')

common/utils/store/schemas/question.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export class Question extends Realm.Object {
77
_id: { type: 'objectId', default: () => new Realm.BSON.ObjectId() },
88
id: 'string',
99
mode: 'string',
10-
slug: 'string',
11-
title: 'string',
12-
detail: 'string',
10+
slug: 'string?',
11+
title: 'string?',
12+
detail: 'string?',
13+
jsonExampleTestcases: 'string?',
14+
exampleTestcases: 'string?',
1315
lang: 'string',
1416
code: 'string?',
1517
url: 'string?',

0 commit comments

Comments
 (0)