Skip to content

Commit 0a3e5d8

Browse files
committed
파일 내용의 백틱과 충돌하지 않도록 코드 fence 길이 조정
파일 내용에 포함된 가장 긴 연속 백틱보다 긴 fence를 생성하여 분석 댓글의 코드 블록이 중간에 닫히지 않도록 한다.
1 parent f99b314 commit 0a3e5d8

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎handlers/tag-patterns.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
import { getGitHubHeaders } from "../utils/github.js";
15+
import { createCodeFence } from "../utils/markdown.js";
1516
import { hasMaintenanceLabel } from "../utils/validation.js";
1617
import { generatePatternAnalysis } from "../utils/openai.js";
1718
import {
@@ -217,13 +218,14 @@ ${renderAnalyzedSource(file.filename, fileContent, isContentTruncated)}
217218
function renderAnalyzedSource(filename, content, isContentTruncated) {
218219
const language = filename.includes(".") ? filename.split(".").pop() : "";
219220
const truncationNotice = isContentTruncated ? "\n... (이하 생략)" : "";
221+
const codeFence = createCodeFence(content);
220222

221223
return `<details>
222224
<summary>${filename}</summary>
223225
224-
\`\`\`${language}
226+
${codeFence}${language}
225227
${content}${truncationNotice}
226-
\`\`\`
228+
${codeFence}
227229
228230
</details>`;
229231
}

‎utils/markdown.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* GitHub Flavored Markdown(GFM) 유틸리티
3+
*
4+
* @see https://github.github.com/gfm/
5+
*/
6+
7+
/**
8+
* 콘텐츠 내부의 연속된 백틱보다 긴 GFM 코드 fence를 생성한다.
9+
*
10+
* @param {string} content
11+
* @returns {string}
12+
* @see https://github.github.com/gfm/#fenced-code-blocks
13+
*/
14+
export function createCodeFence(content) {
15+
const longestBacktickLength = (content.match(/`+/g) ?? []).reduce(
16+
(maxLength, backticks) => Math.max(maxLength, backticks.length),
17+
0
18+
);
19+
20+
const fenceLength = Math.max(3, longestBacktickLength + 1);
21+
return "`".repeat(fenceLength);
22+
}

‎utils/markdown.test.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import { createCodeFence } from "./markdown.js";
4+
5+
const backticks = (count) => "`".repeat(count);
6+
7+
describe("createCodeFence", () => {
8+
it.each([
9+
["없으면", "plain text"],
10+
["1개면", backticks(1)],
11+
["2개면", backticks(2)],
12+
])("연속 백틱이 %s 길이 3인 fence를 반환한다", (_case, content) => {
13+
expect(createCodeFence(content)).toBe(backticks(3));
14+
});
15+
16+
it.each([3, 4, 5, 6])(
17+
"연속 백틱이 %i개면 하나 더 긴 fence를 반환한다",
18+
(count) => {
19+
expect(createCodeFence(backticks(count))).toBe(backticks(count + 1));
20+
}
21+
);
22+
23+
it.each([
24+
`${backticks(5)}\n\n${backticks(3)}`,
25+
`${backticks(3)}\n\n${backticks(5)}\n\n${backticks(3)}`,
26+
`${backticks(3)}\n\n${backticks(5)}`,
27+
])(
28+
"여러 묶음은 위치와 상관없이 가장 긴 연속 백틱을 기준으로 한다",
29+
(content) => {
30+
expect(createCodeFence(content)).toBe(backticks(6));
31+
}
32+
);
33+
});

0 commit comments

Comments
 (0)