Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | 1x 13x 13x 13x 19x 19x 19x 19x 19x 19x 7x 7x 7x 1x 6x 7x 12x 12x 19x 19x 8x 8x 2x 2x 19x 11x 11x 11x 11x 11x 11x 10x 10x 10x 10x 10x 10x 11x 1x 1x 1x 11x 19x 1x | #!/usr/bin/env -S npx tsx
/**
* Fix Article Navigation: Language Switcher + Back-to-News Top Nav
*
* This script ensures ALL news articles have:
* 1. A language switcher nav (14 languages) after <body>
* 2. An article-top-nav div with a localized back-to-news link before the article
*
* It auto-discovers all articles in the news/ directory and processes them
* idempotently — safe to run multiple times.
*
* Usage:
* npx tsx scripts/fix-article-navigation.ts
* npx tsx scripts/fix-article-navigation.ts --dry-run
*
* @author Hack23 AB
* @license Apache-2.0
*/
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import type { Language } from './types/language.js';
import { ALL_LANG_CODES, FOOTER_LABELS } from './article-template/constants.js';
import { getNewsIndexFilename, generateArticleLanguageSwitcher } from './article-template/helpers.js';
// ── Helpers ───────────────────────────────────────────────────────────────
function extractLang(filename: string): Language | null {
const name = filename.replace(/\.html$/, '');
for (const lang of ALL_LANG_CODES) {
if (name.endsWith(`-${lang}`)) return lang;
}
return null;
}
function extractBase(filename: string): string | null {
const name = filename.replace(/\.html$/, '');
for (const lang of ALL_LANG_CODES) {
if (name.endsWith(`-${lang}`)) return name.slice(0, -(lang.length + 1));
}
return null;
}
function generateTopNav(lang: Language): string {
const label = FOOTER_LABELS[lang].backToNews;
const index = getNewsIndexFilename(lang);
return `\n<div class="article-top-nav">\n <a href="${index}" class="back-to-news">\n ← ${label}\n </a>\n</div>\n`;
}
// ── Processing ────────────────────────────────────────────────────────────
export interface ProcessResult {
addedSwitcher: boolean;
addedTopnav: boolean;
fixedTopnav: boolean;
}
/**
* Transform article HTML string: ensures language-switcher and article-top-nav
* with a back-to-news link are present. Idempotent — safe to call multiple times.
*
* @param content Original HTML string
* @param baseSlug Article base slug without directory prefix (e.g. "2026-01-01-article")
* @param lang Language code for this variant
* @returns Updated HTML string and flags indicating what changed
*/
export function transformContent(
content: string,
baseSlug: string,
lang: Language,
): { content: string } & ProcessResult {
let result = content;
let addedSwitcher = false;
let addedTopnav = false;
let fixedTopnav = false;
// ── 1. Language switcher ──────────────────────────────────────────
const hasSwitcher = result.includes('language-switcher');
if (!hasSwitcher) {
const switcherHtml = generateArticleLanguageSwitcher(baseSlug, lang);
// Prefer inserting AFTER the skip-link so it remains the first focusable element.
const skipLinkPattern = /(<a[^>]*class="skip-link"[^>]*>[\s\S]*?<\/a>)/;
if (skipLinkPattern.test(result)) {
result = result.replace(skipLinkPattern, `$1\n${switcherHtml}`);
} else {
result = result.replace(/(<body[^>]*>)/, `$1\n${switcherHtml}`);
}
addedSwitcher = true;
} else {
// Update existing switcher to have all 14 languages.
// Use [^\S\n]* (spaces/tabs, not newlines) to consume any indentation before <nav>,
// so the replacement string's own leading spaces don't accumulate on repeated runs.
const newSwitcher = generateArticleLanguageSwitcher(baseSlug, lang);
result = result.replace(/[^\S\n]*<nav class="language-switcher"[^>]*>[\s\S]*?<\/nav>/, newSwitcher);
}
// ── 2. article-top-nav ────────────────────────────────────────────
const hasTopnav = result.includes('article-top-nav');
// If top-nav exists but is missing back-to-news link, replace it
if (hasTopnav) {
const topNavHasBackLink =
/<div class="article-top-nav">[\s\S]*?class="back-to-news"[\s\S]*?<\/div>/.test(result);
if (!topNavHasBackLink) {
result = result.replace(
/<div class="article-top-nav">[\s\S]*?<\/div>/,
generateTopNav(lang).trim(),
);
fixedTopnav = true;
}
}
if (!hasTopnav) {
const topNavHtml = generateTopNav(lang);
let inserted = false;
// Pattern A: insert after closing </nav> of language-switcher, before article/div.news-article
Eif (result.includes('</nav>')) {
const navPattern = /((<\/nav>)([\s]*)(<(?:article|div)\s+class="(?:news-article|container)"))/s;
const match = navPattern.exec(result);
if (match) {
const endOfNav = match.index + match[2].length;
const whitespace = match[3];
const articleTag = match[4];
const afterFull = result.slice(match.index + match[0].length);
result = result.slice(0, endOfNav) + topNavHtml + whitespace + articleTag + afterFull;
inserted = true;
}
}
// Pattern B: insert directly before <article class="news-article"> or <div class="container">
if (!inserted) {
const articlePattern = /(<(?:article|div)\s+class="(?:news-article|container)")/;
const match = articlePattern.exec(result);
Iif (match) {
result = result.slice(0, match.index) + topNavHtml + '\n' + result.slice(match.index);
inserted = true;
}
}
if (inserted) addedTopnav = true;
}
return { content: result, addedSwitcher, addedTopnav, fixedTopnav };
}
function processArticle(filepath: string, baseSlug: string, lang: Language, dryRun: boolean): ProcessResult {
const original = fs.readFileSync(filepath, 'utf-8');
const { content, addedSwitcher, addedTopnav, fixedTopnav } = transformContent(original, baseSlug, lang);
// ── Write if changed ──────────────────────────────────────────────
if (content !== original && !dryRun) {
fs.writeFileSync(filepath, content, 'utf-8');
}
return { addedSwitcher, addedTopnav, fixedTopnav };
}
interface ArticleMap {
[baseSlug: string]: Partial<Record<Language, string>>;
}
function discoverArticles(newsDir: string): ArticleMap {
const articles: ArticleMap = {};
const files = fs.readdirSync(newsDir).sort();
for (const name of files) {
if (!name.endsWith('.html') || name.startsWith('index')) continue;
const lang = extractLang(name);
const base = extractBase(name);
if (lang && base) {
if (!articles[base]) articles[base] = {};
articles[base][lang] = path.join(newsDir, name);
}
}
return articles;
}
function main(): void {
const dryRun = process.argv.includes('--dry-run');
if (dryRun) {
console.log('=== DRY RUN — no files will be modified ===\n');
}
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const newsDir = path.resolve(scriptDir, '..', 'news');
if (!fs.existsSync(newsDir)) {
console.error(`ERROR: news directory not found at ${newsDir}`);
process.exit(1);
}
console.log('=== Fix Article Navigation ===');
console.log(`News directory: ${newsDir}\n`);
const articles = discoverArticles(newsDir);
const slugs = Object.keys(articles).sort();
console.log(`Discovered ${slugs.length} unique article slugs\n`);
let total = 0;
let switchersAdded = 0;
let topnavsAdded = 0;
let topnavsFixed = 0;
for (const baseSlug of slugs) {
const langFiles = articles[baseSlug];
for (const lang of ALL_LANG_CODES) {
const filepath = langFiles[lang];
if (!filepath) continue;
total++;
const { addedSwitcher, addedTopnav, fixedTopnav } = processArticle(filepath, baseSlug, lang, dryRun);
if (addedSwitcher) switchersAdded++;
if (addedTopnav) topnavsAdded++;
if (fixedTopnav) topnavsFixed++;
}
}
console.log('=== Summary ===');
console.log(`Total files processed: ${total}`);
console.log(`Language switchers added: ${switchersAdded}`);
console.log(`Top nav (article-top-nav) added: ${topnavsAdded}`);
console.log(`Top nav fixed (missing back-to-news link): ${topnavsFixed}`);
if (dryRun) {
console.log('\n(Dry run — no files were modified)');
}
console.log('\n✓ Done!');
}
Iif (import.meta.url === `file://${process.argv[1]}`) {
main();
}
|