Skip to content

Commit 1f46457

Browse files
committed
chore: Improve readability by using better variable names.
1 parent 11f535e commit 1f46457

32 files changed

Lines changed: 329 additions & 276 deletions

‎packages/md/src/compile.ts‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async function getCompiler(options?: CompileOptions): Promise<MarkdownExit> {
5252
}
5353

5454
async function createCompiler(options?: CompileOptions): Promise<MarkdownExit> {
55-
const md = new MarkdownExit({ html: true, linkify: true });
55+
const markdown = new MarkdownExit({ html: true, linkify: true });
5656

5757
// Shiki highlighting with JS RegExp engine (no WASM) + lazy language loading
5858
const themes = options?.shiki?.themes ?? {
@@ -71,8 +71,8 @@ async function createCompiler(options?: CompileOptions): Promise<MarkdownExit> {
7171
transformerNotationFocus(),
7272
transformerNotationErrorLevel(),
7373
];
74-
md.renderer.rules.fence = async (tokens: ReadonlyArray<Token>, idx: number) => {
75-
const token = tokens[idx];
74+
markdown.renderer.rules.fence = async (tokens: ReadonlyArray<Token>, tokenIndex: number) => {
75+
const token = tokens[tokenIndex];
7676
const code = token.content;
7777
let lang = token.info ? token.info.split(/\s+/g)[0] : 'text';
7878
if (!isSpecialLang(lang) && !highlighter.getLoadedLanguages().includes(lang)) {
@@ -92,49 +92,49 @@ async function createCompiler(options?: CompileOptions): Promise<MarkdownExit> {
9292
};
9393

9494
// Code block wrapping (must come after shiki, before line-numbers)
95-
md.use(preWrapperPlugin);
95+
markdown.use(preWrapperPlugin);
9696

9797
// Container blocks: :::tip, :::warning, etc.
98-
md.use(containerPlugin);
98+
markdown.use(containerPlugin);
9999

100100
// Image path normalization + lazy loading
101-
md.use(imagePlugin, { lazyLoading: true });
101+
markdown.use(imagePlugin, { lazyLoading: true });
102102

103103
// Link handling: external attrs, .md normalization
104-
md.use(linkPlugin);
104+
markdown.use(linkPlugin);
105105

106106
// Line numbers (opt-in per code block by default)
107-
md.use(lineNumberPlugin, false);
107+
markdown.use(lineNumberPlugin, false);
108108

109109
// GitHub alerts: > [!NOTE], > [!TIP], etc.
110-
md.use(gitHubAlertsPlugin);
110+
markdown.use(gitHubAlertsPlugin);
111111

112112
// Attribute syntax: {.class #id}
113-
md.use(attrs);
113+
markdown.use(attrs);
114114

115115
// Emoji shortcodes
116-
md.use(emoji);
116+
markdown.use(emoji);
117117

118118
// Heading anchors
119119
// @ts-expect-error markdown-it-anchor types reference @types/markdown-it, structurally incompatible with markdown-exit
120-
md.use(anchor, {
120+
markdown.use(anchor, {
121121
permalink: anchor.permalink.ariaHidden({ placement: 'before' }),
122122
});
123123

124124
// [[toc]] directive
125-
md.use(tocPlugin);
125+
markdown.use(tocPlugin);
126126

127-
return md;
127+
return markdown;
128128
}
129129

130130
function extractHeadings(tokens: ReadonlyArray<Token>): ReadonlyArray<Heading> {
131131
const headings: Array<Heading> = [];
132-
for (let i = 0; i < tokens.length; i++) {
133-
const token = tokens[i];
132+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
133+
const token = tokens[tokenIndex];
134134
if (token.type === 'heading_open') {
135135
const depth = Number(token.tag.slice(1));
136136
const slug = token.attrGet?.('id') ?? '';
137-
const inline = tokens[i + 1];
137+
const inline = tokens[tokenIndex + 1];
138138
const text = (
139139
inline?.children
140140
?.filter((child) => child.type === 'text' || child.type === 'code_inline')
@@ -156,7 +156,8 @@ export async function compile(source: string, options?: CompileOptions): Promise
156156
const headings = extractHeadings(tokens);
157157
const html = await compiler.renderAsync(content, env);
158158

159-
const title = (frontmatter.title as string) ?? headings.find((h) => h.depth === 1)?.text ?? '';
159+
const title =
160+
(frontmatter.title as string) ?? headings.find((heading) => heading.depth === 1)?.text ?? '';
160161

161162
return { html, frontmatter, headings, title };
162163
}

‎packages/md/src/emit-react.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ export function emitReactComponentWithIslands(
3333
let code = `import React from "react";\n`;
3434
code += `import { FrontmatterContext } from "@void/md";\n`;
3535

36-
for (let i = 0; i < islandImports.length; i++) {
37-
const item = islandImports[i];
38-
code += `import _raw_island_${i} from ${JSON.stringify(item.specifier)};\n`;
36+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
37+
const islandImport = islandImports[islandIndex];
38+
code += `import _raw_island_${islandIndex} from ${JSON.stringify(islandImport.specifier)};\n`;
3939
}
4040

41-
for (let i = 0; i < islandImports.length; i++) {
42-
const item = islandImports[i];
43-
code += `function ${item.name}(props) {
41+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
42+
const islandImport = islandImports[islandIndex];
43+
code += `function ${islandImport.name}(props) {
4444
return React.createElement(
4545
"div",
4646
{
47-
"data-island": ${JSON.stringify(toIslandId(item))},
47+
"data-island": ${JSON.stringify(toIslandId(islandImport))},
4848
"data-props": JSON.stringify(props),
49-
"data-hydrate": ${JSON.stringify(item.strategy)}
49+
"data-hydrate": ${JSON.stringify(islandImport.strategy)}
5050
},
51-
React.createElement(_raw_island_${i}, props)
51+
React.createElement(_raw_island_${islandIndex}, props)
5252
);
5353
}\n`;
5454
}

‎packages/md/src/emit-solid.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ export function emitSolidComponentWithIslands(
3232

3333
let code = `import { FrontmatterContext } from "@void/md";\n`;
3434

35-
for (let i = 0; i < islandImports.length; i++) {
36-
const item = islandImports[i];
37-
const alias = `RawIsland${i}`;
38-
importAliasByName.set(item.name, alias);
39-
code += `import ${alias} from ${JSON.stringify(item.specifier)};\n`;
35+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
36+
const islandImport = islandImports[islandIndex];
37+
const alias = `RawIsland${islandIndex}`;
38+
importAliasByName.set(islandImport.name, alias);
39+
code += `import ${alias} from ${JSON.stringify(islandImport.specifier)};\n`;
4040
}
4141

4242
code += `

‎packages/md/src/emit-svelte.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export const headings = ${headings};
4646
import { setFrontmatter } from "@void/md";
4747
`;
4848

49-
for (let i = 0; i < islandImports.length; i++) {
50-
const item = islandImports[i];
51-
const alias = `RawIsland${i}`;
52-
importAliasByName.set(item.name, alias);
53-
source += `import ${alias} from ${JSON.stringify(item.specifier)};
49+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
50+
const islandImport = islandImports[islandIndex];
51+
const alias = `RawIsland${islandIndex}`;
52+
importAliasByName.set(islandImport.name, alias);
53+
source += `import ${alias} from ${JSON.stringify(islandImport.specifier)};
5454
`;
5555
}
5656

‎packages/md/src/emit-vue.ts‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ type IslandMatch = {
3737

3838
function findIslandTags(html: string, names: Set<string>): ReadonlyArray<IslandMatch> {
3939
const matches: Array<IslandMatch> = [];
40-
let m;
40+
let tagMatch: RegExpExecArray | null;
4141
ISLAND_TAG_RE.lastIndex = 0;
42-
while ((m = ISLAND_TAG_RE.exec(html)) !== null) {
43-
const name = m[1] ?? m[3];
42+
while ((tagMatch = ISLAND_TAG_RE.exec(html)) !== null) {
43+
const name = tagMatch[1] ?? tagMatch[3];
4444
if (names.has(name)) {
4545
matches.push({
4646
name,
47-
start: m.index,
48-
end: m.index + m[0].length,
49-
attrs: (m[2] ?? m[4] ?? '').trim(),
50-
children: m[5] ?? '',
47+
start: tagMatch.index,
48+
end: tagMatch.index + tagMatch[0].length,
49+
attrs: (tagMatch[2] ?? tagMatch[4] ?? '').trim(),
50+
children: tagMatch[5] ?? '',
5151
});
5252
}
5353
}
@@ -56,10 +56,10 @@ function findIslandTags(html: string, names: Set<string>): ReadonlyArray<IslandM
5656

5757
function parseAttrs(attrStr: string): Record<string, string> {
5858
const result: Record<string, string> = {};
59-
const re = /(\w[\w-]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
60-
let m;
61-
while ((m = re.exec(attrStr)) !== null) {
62-
result[m[1]] = m[2] ?? m[3] ?? m[4] ?? '';
59+
const attrRe = /(\w[\w-]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+)))?/g;
60+
let attrMatch: RegExpExecArray | null;
61+
while ((attrMatch = attrRe.exec(attrStr)) !== null) {
62+
result[attrMatch[1]] = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4] ?? '';
6363
}
6464
return result;
6565
}
@@ -68,27 +68,27 @@ export function emitVueComponentWithIslands(
6868
result: CompileResult,
6969
islandImports: ReadonlyArray<IslandImport>,
7070
): string {
71-
const nameSet = new Set(islandImports.map((i) => i.name));
71+
const nameSet = new Set(islandImports.map((islandImport) => islandImport.name));
7272
const islandTags = findIslandTags(result.html, nameSet);
7373

7474
let code = `import { defineComponent, h, createStaticVNode, provide } from "vue";\n`;
7575

7676
// Import raw island components
77-
for (let i = 0; i < islandImports.length; i++) {
78-
const imp = islandImports[i];
79-
code += `import _raw_island_${i} from ${JSON.stringify(imp.specifier)};\n`;
77+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
78+
const islandImport = islandImports[islandIndex];
79+
code += `import _raw_island_${islandIndex} from ${JSON.stringify(islandImport.specifier)};\n`;
8080
}
8181

8282
// Create island wrapper components
83-
for (let i = 0; i < islandImports.length; i++) {
84-
const imp = islandImports[i];
85-
code += `const ${imp.name} = { inheritAttrs: false, render() {
83+
for (let islandIndex = 0; islandIndex < islandImports.length; islandIndex++) {
84+
const islandImport = islandImports[islandIndex];
85+
code += `const ${islandImport.name} = { inheritAttrs: false, render() {
8686
const props = this.$attrs;
8787
return h("div", {
88-
"data-island": ${JSON.stringify(toIslandId(imp))},
88+
"data-island": ${JSON.stringify(toIslandId(islandImport))},
8989
"data-props": JSON.stringify(props),
90-
"data-hydrate": ${JSON.stringify(imp.strategy)}
91-
}, h(_raw_island_${i}, props));
90+
"data-hydrate": ${JSON.stringify(islandImport.strategy)}
91+
}, h(_raw_island_${islandIndex}, props));
9292
}};\n`;
9393
}
9494

‎packages/md/src/extract-script.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export function extractScript(source: string): ScriptExtraction {
2929
const body = source.slice(match[0].length);
3030

3131
const islandImports: Array<IslandImport> = [];
32-
let m;
33-
while ((m = ISLAND_IMPORT_RE.exec(script)) !== null) {
32+
let importMatch: RegExpExecArray | null;
33+
while ((importMatch = ISLAND_IMPORT_RE.exec(script)) !== null) {
3434
islandImports.push({
35-
name: m[1],
36-
specifier: m[2].slice(1, -1),
37-
strategy: m[3].slice(1, -1),
35+
name: importMatch[1],
36+
specifier: importMatch[2].slice(1, -1),
37+
strategy: importMatch[3].slice(1, -1),
3838
});
3939
}
4040

‎packages/md/src/metadata.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ function getMetadataCompiler(): MarkdownExit {
1010
if (metadataCompiler) {
1111
return metadataCompiler;
1212
}
13-
const md = new MarkdownExit({ html: true, linkify: true });
13+
const markdown = new MarkdownExit({ html: true, linkify: true });
1414
// @ts-expect-error markdown-it-anchor types reference @types/markdown-it, structurally incompatible with markdown-exit
15-
md.use(anchor, {
15+
markdown.use(anchor, {
1616
permalink: anchor.permalink.ariaHidden({ placement: 'before' }),
1717
});
18-
metadataCompiler = md;
19-
return md;
18+
metadataCompiler = markdown;
19+
return markdown;
2020
}
2121

2222
function extractHeadings(tokens: ReadonlyArray<Token>): ReadonlyArray<Heading> {
2323
const headings: Array<Heading> = [];
24-
for (let i = 0; i < tokens.length; i++) {
25-
const token = tokens[i];
24+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
25+
const token = tokens[tokenIndex];
2626
if (token.type === 'heading_open') {
2727
const depth = Number(token.tag.slice(1));
2828
const slug = token.attrGet?.('id') ?? '';
29-
const inline = tokens[i + 1];
29+
const inline = tokens[tokenIndex + 1];
3030
const text = (
3131
inline?.children
32-
?.filter((t) => t.type === 'text' || t.type === 'code_inline')
33-
.map((t) => t.content)
32+
?.filter((child) => child.type === 'text' || child.type === 'code_inline')
33+
.map((child) => child.content)
3434
.join('') ?? ''
3535
).trim();
3636
headings.push({ depth, slug, text });
@@ -53,7 +53,8 @@ export function extractMetadata(source: string): MetadataResult {
5353
const tokens = compiler.parse(content, env);
5454
const headings = extractHeadings(tokens);
5555

56-
const title = (frontmatter.title as string) ?? headings.find((h) => h.depth === 1)?.text ?? '';
56+
const title =
57+
(frontmatter.title as string) ?? headings.find((heading) => heading.depth === 1)?.text ?? '';
5758

5859
return { frontmatter, headings, title };
5960
}

‎packages/md/src/plugin.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export function voidMarkdown(options?: MarkdownOptions): ReadonlyArray<Plugin> {
123123
};
124124
}),
125125
);
126-
return pages.sort((a, b) => a.path.localeCompare(b.path));
126+
return pages.sort((leftPage, rightPage) => leftPage.path.localeCompare(rightPage.path));
127127
}
128128

129129
function writeMdTypes() {

‎packages/md/src/plugins/github-alerts.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ export function gitHubAlertsPlugin(md: MarkdownExit, options?: GitHubAlertsOptio
3838

3939
const rule: RuleCore = (state) => {
4040
const tokens = state.tokens as ReadonlyArray<AlertToken>;
41-
for (let i = 0; i < tokens.length; i++) {
42-
if (tokens[i].type === 'blockquote_open') {
43-
const startIndex = i;
41+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
42+
if (tokens[tokenIndex].type === 'blockquote_open') {
43+
const startIndex = tokenIndex;
4444
const open = tokens[startIndex];
45-
let endIndex = i + 1;
45+
let endIndex = tokenIndex + 1;
4646
while (
4747
endIndex < tokens.length &&
4848
(tokens[endIndex].type !== 'blockquote_close' || tokens[endIndex].level !== open.level)

‎packages/md/src/plugins/heading-extract.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export function headingExtractPlugin(md: MarkdownExit): void {
1919
const render = md.renderer.render.bind(md.renderer);
2020
md.renderer.render = (tokens: Array<Token>, options: RenderOptions, env: MarkdownEnv = {}) => {
2121
env.headings = [];
22-
for (let i = 0; i < tokens.length; i++) {
23-
const token = tokens[i];
22+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) {
23+
const token = tokens[tokenIndex];
2424
if (token.type === 'heading_open') {
2525
const depth = Number(token.tag.slice(1));
2626
const slug = token.attrGet?.('id') ?? '';
27-
const inline = tokens[i + 1];
27+
const inline = tokens[tokenIndex + 1];
2828
const text = (
2929
inline?.children
3030
?.filter((child) => child.type === 'text' || child.type === 'code_inline')

0 commit comments

Comments
 (0)