Skip to content

Commit 5b53e79

Browse files
committed
getCodeBlocks()のfilter処理のrefactoring
1 parent ca6e48c commit 5b53e79

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

rest/getCodeBlocks.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,8 @@ export const getCodeBlocks = async (
5353
let currentCode: CodeTitle & {
5454
/** 読み取り中の行がコードブロックかどうか */
5555
isCodeBlock: boolean;
56-
/** 読み取り中のコードブロックを保存するかどうか */
57-
isCollect: boolean;
5856
} = {
5957
isCodeBlock: false,
60-
isCollect: false,
6158
filename: "",
6259
lang: "",
6360
indent: 0,
@@ -66,23 +63,18 @@ export const getCodeBlocks = async (
6663
if (currentCode.isCodeBlock) {
6764
const body = extractFromCodeBody(line.text, currentCode.indent);
6865
if (body === null) {
69-
if (currentCode.isCollect) {
70-
codeBlocks[codeBlocks.length - 1].nextLine = line;
71-
}
66+
codeBlocks[codeBlocks.length - 1].nextLine = line;
7267
currentCode.isCodeBlock = false;
7368
continue;
7469
}
75-
if (!currentCode.isCollect) continue;
7670
codeBlocks[codeBlocks.length - 1].bodyLines.push(line);
7771
} else {
7872
const matched = extractFromCodeTitle(line.text);
7973
if (matched === null) {
8074
currentCode.isCodeBlock = false;
8175
continue;
8276
}
83-
const isCollect = isMatchFilter(matched, filter);
84-
currentCode = { isCodeBlock: true, isCollect: isCollect, ...matched };
85-
if (!currentCode.isCollect) continue;
77+
currentCode = { isCodeBlock: true, ...matched };
8678
codeBlocks.push({
8779
filename: currentCode.filename,
8880
lang: currentCode.lang,
@@ -96,7 +88,7 @@ export const getCodeBlocks = async (
9688
});
9789
}
9890
}
99-
return codeBlocks;
91+
return codeBlocks.filter((codeBlock) => isMatchFilter(codeBlock, filter));
10092
};
10193

10294
/** targetを`Line[]`に変換する */
@@ -111,13 +103,13 @@ async function getLines(
111103
}
112104
}
113105

114-
/** コードタイトルのフィルターを検証する */
106+
/** コードブロックのフィルターに合致しているか検証する */
115107
function isMatchFilter(
116-
codeTitle: CodeTitle,
108+
codeBlock: TinyCodeBlock,
117109
filter?: GetCodeBlocksFilter,
118110
): boolean {
119-
if (filter?.filename && filter.filename !== codeTitle.filename) return false;
120-
if (filter?.lang && filter.lang !== codeTitle.lang) return false;
111+
if (filter?.filename && filter.filename !== codeBlock.filename) return false;
112+
if (filter?.lang && filter.lang !== codeBlock.lang) return false;
121113
return true;
122114
}
123115

0 commit comments

Comments
 (0)