@@ -15,7 +15,7 @@ export interface Highlighter {
1515 function resolveTheme(options?: Options): Theme {
1616 const t = options?.theme || "16";
1717 if (typeof t === "string") {
18- return (themes[t] || themes["none"]) as Theme;
18+ return (themes[t] || themes.none) as Theme;
1919 }
2020 return t as Theme;
2121 }
@@ -32,21 +32,20 @@ export function createHighlighter(options?: Options): Highlighter {
3232 let inCodeBlock = false;
3333
3434 function parseInline(text: string): string {
35-
3635 let result = "";
3736 let i = 0;
3837 while (i < text.length) {
3938 if (text.startsWith("`", i) && !text.startsWith("``", i)) {
40- let end = text.indexOf("`", i + 1);
39+ const end = text.indexOf("`", i + 1);
4140 if (end !== -1) {
4241 result += theme.code(`\`${text.slice(i + 1, end)}\``);
4342 i = end + 1;
4443 continue;
4544 }
4645 }
47-
46+
4847 if (text.startsWith("**", i)) {
49- let end = text.indexOf("**", i + 2);
48+ const end = text.indexOf("**", i + 2);
5049 if (end !== -1) {
5150 result += theme.bold(`**${text.slice(i + 2, end)}**`);
5251 i = end + 2;
@@ -55,7 +54,7 @@ export function createHighlighter(options?: Options): Highlighter {
5554 }
5655
5756 if (text.startsWith("*", i) && !text.startsWith("**", i)) {
58- let end = text.indexOf("*", i + 1);
57+ const end = text.indexOf("*", i + 1);
5958 if (end !== -1 && !text.startsWith("**", end)) {
6059 result += theme.italic(`*${text.slice(i + 1, end)}*`);
6160 i = end + 1;
@@ -64,11 +63,13 @@ export function createHighlighter(options?: Options): Highlighter {
6463 }
6564
6665 if (text.startsWith("[", i)) {
67- let bracketEnd = text.indexOf("]", i + 1);
66+ const bracketEnd = text.indexOf("]", i + 1);
6867 if (bracketEnd !== -1 && text.charAt(bracketEnd + 1) === "(") {
69- let parenEnd = text.indexOf(")", bracketEnd + 2);
68+ const parenEnd = text.indexOf(")", bracketEnd + 2);
7069 if (parenEnd !== -1) {
71- result += theme.link(`[${text.slice(i + 1, bracketEnd)}](${text.slice(bracketEnd + 2, parenEnd)})`);
70+ result += theme.link(
71+ `[${text.slice(i + 1, bracketEnd)}](${text.slice(bracketEnd + 2, parenEnd)})`,
72+ );
7273 i = parenEnd + 1;
7374 continue;
7475 }
@@ -78,11 +79,11 @@ export function createHighlighter(options?: Options): Highlighter {
7879 result += text[i];
7980 i++;
8081 }
81-
82+
8283 return result;
8384 }
8485
85- function parseLine(line: string, isLastLine: boolean): string {
86+ function parseLine(line: string, _isLastLine: boolean): string {
8687 if (inCodeBlock) {
8788 if (line.trim().startsWith("```")) {
8889 inCodeBlock = false;
@@ -103,24 +104,24 @@ export function createHighlighter(options?: Options): Highlighter {
103104
104105 const headingMatch = line.match(/^(#{1,3})\s+(.*)$/);
105106 if (headingMatch) {
106- const hashes = headingMatch[1]!;
107- const text = headingMatch[2]!;
107+ const hashes = headingMatch[1] ?? "";
108+ const text = headingMatch[2] ?? "";
108109 return theme.heading(`${hashes} ${parseInline(text)}`);
109110 }
110111
111112 const bqMatch = line.match(/^(>\s+)(.*)$/);
112113 if (bqMatch) {
113- return theme.blockquote(bqMatch[1]!) + parseInline(bqMatch[2]!);
114+ return theme.blockquote(bqMatch[1] ?? "") + parseInline(bqMatch[2] ?? "");
114115 }
115116
116117 const ulMatch = line.match(/^([-*]\s+)(.*)$/);
117118 if (ulMatch) {
118- return theme.listMarker(ulMatch[1]!) + parseInline(ulMatch[2]!);
119+ return theme.listMarker(ulMatch[1] ?? "") + parseInline(ulMatch[2] ?? "");
119120 }
120121
121122 const olMatch = line.match(/^(\d+\.\s+)(.*)$/);
122123 if (olMatch) {
123- return theme.listMarker(olMatch[1]!) + parseInline(olMatch[2]!);
124+ return theme.listMarker(olMatch[1] ?? "") + parseInline(olMatch[2] ?? "");
124125 }
125126
126127 return parseInline(line);
@@ -128,13 +129,17 @@ export function createHighlighter(options?: Options): Highlighter {
128129
129130 return {
130131 write(chunk: string | Uint8Array): string {
131- const text = typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk);
132+ const text =
133+ typeof chunk === "string" ? chunk : new TextDecoder().decode(chunk);
132134 buffer += text;
133-
135+
134136 const lines = buffer.split("\n");
135137 buffer = lines.pop() ?? "";
136-
137- return lines.map((line) => parseLine(line, false)).join("\n") + (lines.length > 0 ? "\n" : "");
138+
139+ return (
140+ lines.map((line) => parseLine(line, false)).join("\n") +
141+ (lines.length > 0 ? "\n" : "")
142+ );
138143 },
139144 end(): string {
140145 if (buffer.length > 0) {
@@ -143,6 +148,6 @@ export function createHighlighter(options?: Options): Highlighter {
143148 return out;
144149 }
145150 return "";
146- }
151+ },
147152 };
148153 }