Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .remarkrc.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import remarkPresetLintRecommended from 'remark-preset-lint-recommended';
// @ts-check

import remarkFrontmatter from 'remark-frontmatter';
import remarkMdx from 'remark-mdx';
import remarkPresetLintRecommended from 'remark-preset-lint-recommended';
import remarkNoInlineCodeFences from './src/plugins/remark-no-inline-code-fences.mjs';
import remarkNoHtmlLinks from './src/plugins/remark-no-html-links.mjs';
import remarkLintNoDeadUrls from 'remark-lint-no-dead-urls';
Expand Down
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import remarkGlossary from './src/plugins/remark-glossary';
import remarkCenter from './src/plugins/remark-center';
import remarkFigure from './src/plugins/remark-figure';
import remarkImageAttributes from './src/plugins/remark-image-attributes';
import { remarkMdxGlobalImports } from './src/plugins/remark-mdx-global-imports.ts';
import { remarkMdxGlobalImports } from './src/plugins/remark-mdx-global-imports';
import remarkCodeRegion from './src/plugins/remark-code-region';

export default defineConfig({
Expand Down
4 changes: 1 addition & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
{ argsIgnorePattern: '^_' },
],
'@typescript-eslint/no-explicit-any': 'error',
'no-undef': 'off',
},
},

Expand All @@ -28,8 +29,5 @@ export default [
ImageMetadata: 'readonly',
},
},
rules: {
'no-undef': 'off',
},
},
];
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"astro-eslint-parser": "^1.4.0",
"eslint": "^10.5.0",
"eslint-plugin-astro": "^1.7.0",
"mdast-util-mdx": "^3.0.0",
"husky": "^9.1.7",
"lint-staged": "^17.0.8",
"mdast-util-mdx-jsx": "^3.2.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 15 additions & 14 deletions scripts/validate-regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ function parseCodeRegionSources(content: string): Map<string, string> {
const frontmatterMatch = content.match(FRONTMATTER_RE);
if (!frontmatterMatch) return sources;

const lines = frontmatterMatch[1].split('\n');
const lines = frontmatterMatch[1]!.split('\n');
const startIdx = lines.findIndex((line) =>
CODE_REGION_SOURCES_KEY_RE.test(line),
);
if (startIdx === -1) return sources;

for (let i = startIdx + 1; i < lines.length; i++) {
const line = lines[i];
for (const line of lines.slice(startIdx + 1)) {
// Stop once we hit a line that isn't indented (end of the map).
if (!/^\s+\S/.test(line)) break;

const m = line.match(CODE_REGION_SOURCE_ENTRY_RE);
if (m) {
sources.set(m[1], m[2]);
sources.set(m[1]!, m[2]!);
}
}

Expand Down Expand Up @@ -89,7 +88,7 @@ function walkMdx(dir: string) {
if (!referencedRegions.has(filePath)) {
referencedRegions.set(filePath, new Set());
}
referencedRegions.get(filePath)!.add(regionName);
referencedRegions.get(filePath)!.add(regionName!);
}
}
}
Expand All @@ -106,10 +105,11 @@ function validateSource(filePath: string) {
const stack: { name: string; lineNum: number }[] = [];
const rel = relative(EXAMPLES_DIR, filePath).replace(/\\/g, '/');

for (let i = 0; i < lines.length; i++) {
let m = lines[i].match(START_RE);
for (const [i, line] of lines.entries()) {
let m = line.match(START_RE);
if (m) {
const name = m[1];
const name = m.at(1);
if (!name) continue;
if (regions.has(name)) {
errors.push(`${rel}:${i + 1}: Duplicate region "${name}"`);
}
Expand All @@ -118,15 +118,16 @@ function validateSource(filePath: string) {
continue;
}

m = lines[i].match(END_RE);
m = line.match(END_RE);
if (m) {
const name = m[1];
if (stack.length === 0) {
const top = stack.at(-1);
if (top === undefined) {
errors.push(
`${rel}:${i + 1}: Unmatched closing tag [/${name}] — no region opened`,
);
} else if (stack[stack.length - 1].name !== name) {
const expected = stack[stack.length - 1].name;
} else if (top.name !== name) {
const expected = top.name;
errors.push(
`${rel}:${i + 1}: Region mismatch — expected [/${expected}] but found [/${name}]`,
);
Expand Down Expand Up @@ -164,7 +165,7 @@ walkExamples(EXAMPLES_DIR);
for (const [filePath, names] of referencedRegions) {
const defs = definedRegions.get(filePath);
for (const name of names) {
if (!defs || !defs.has(name)) {
if (!defs?.has(name)) {
errors.push(
`${filePath}: Region "${name}" referenced in MDX but not defined in examples/${filePath}`,
);
Expand All @@ -175,7 +176,7 @@ for (const [filePath, names] of referencedRegions) {
for (const [filePath, names] of definedRegions) {
const refs = referencedRegions.get(filePath);
for (const [name, lineNum] of names) {
if (!refs || !refs.has(name)) {
if (!refs?.has(name)) {
errors.push(
`${filePath}:${lineNum}: Orphaned region "${name}" — defined but never referenced in any .mdx file`,
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Slides.astro
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function parseSequentialContent(content: string): ParsedSlide[] {
const srcMatch = tagAttrs.match(/src=["']([^"']+)["']/);
const rawSrc = srcMatch ? srcMatch[1] : '';
const altMatch = tagAttrs.match(/alt=["']([^"']*?)["']/);
const alt = altMatch ? altMatch[1] : '';
const alt = altMatch?.[1] ?? '';

if (!rawSrc) return;

Expand Down
23 changes: 11 additions & 12 deletions src/config/sidebarConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export type SidebarSection = {
items: SidebarItem[];
};

export type Item = {
label: string;
href?: string;
items?: Item[];
};

// Define which URL paths belong to which sidebar section
export const sidebarSections: Record<string, SidebarSection[]> = {
// Home page - minimal sidebar or none
Expand Down Expand Up @@ -341,12 +347,7 @@ export function getSidebarForPath(pathname: string): SidebarSection[] {
}
}

if (bestMatch) {
return sidebarSections[bestMatch];
}

// Default to home (empty sidebar)
return sidebarSections['/'] || [];
return sidebarSections[bestMatch || '/'] ?? sidebarSections['/'] ?? [];
}

/**
Expand Down Expand Up @@ -395,12 +396,10 @@ export function getPrevNextLinks(pathname: string): {
if (currentIndex === -1) {
return { prev: null, next: null };
}

const prev = allLinks.at(currentIndex - 1) ?? null;
const next = allLinks.at(currentIndex + 1) ?? null;
return {
prev: currentIndex > 0 ? allLinks[currentIndex - 1] : null,
next:
currentIndex < allLinks.length - 1
? allLinks[currentIndex + 1]
: null,
prev,
next,
};
}
31 changes: 7 additions & 24 deletions src/plugins/remark-center.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
import { visit } from 'unist-util-visit';
import type { Root } from 'mdast';

interface ContainerDirective {
type: 'containerDirective';
name: string;
attributes?: Record<string, string>;
children: unknown[];
data?: {
hName?: string;
hProperties?: Record<string, unknown>;
};
}

export function remarkCenter() {
return (tree: Root) => {
visit(
tree,
'containerDirective',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(node: any) => {
const dir = node as ContainerDirective;
if (dir.name !== 'center') return;
visit(tree, 'containerDirective', (node) => {
if (node.name !== 'center') return;

dir.data = dir.data || {};
dir.data.hName = 'div';
dir.data.hProperties = dir.data.hProperties || {};
dir.data.hProperties.class = 'centered-content';
},
);
node.data = node.data || {};
node.data.hName = 'div';
node.data.hProperties = node.data.hProperties || {};
node.data.hProperties.class = 'centered-content';
});
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/remark-code-region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function remarkCodeRegion() {
const meta: string = node.meta || '';

const token = meta.match(/^(\S+)/);
if (!token) return;
if (!token?.[1]) return;

const raw = token[1];
const hashIdx = raw.indexOf('#');
Expand Down Expand Up @@ -107,6 +107,6 @@ function dedent(str: string): string {
Math.min(min, l.match(/^[ \t]*/)?.[0].length ?? Infinity),
Infinity,
);
if (indent === 0 || !isFinite(indent)) return str;
if (indent === 0 || !Number.isFinite(indent)) return str;
return lines.map((l) => l.slice(indent)).join('\n');
}
Loading