third-audience-mdx 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dashboard/routes/llms-txt-route.js +16 -4
- package/dist/dashboard/routes/llms-txt-route.js.map +1 -1
- package/dist/dashboard/routes/llms-txt-route.mjs +16 -4
- package/dist/dashboard/routes/llms-txt-route.mjs.map +1 -1
- package/dist/dashboard/routes/markdown-route.js +199 -7
- package/dist/dashboard/routes/markdown-route.js.map +1 -1
- package/dist/dashboard/routes/markdown-route.mjs +206 -7
- package/dist/dashboard/routes/markdown-route.mjs.map +1 -1
- package/dist/dashboard/routes/okf-graph-route.js +16 -4
- package/dist/dashboard/routes/okf-graph-route.js.map +1 -1
- package/dist/dashboard/routes/okf-graph-route.mjs +16 -4
- package/dist/dashboard/routes/okf-graph-route.mjs.map +1 -1
- package/dist/dashboard/routes/okf-route.js +16 -4
- package/dist/dashboard/routes/okf-route.js.map +1 -1
- package/dist/dashboard/routes/okf-route.mjs +16 -4
- package/dist/dashboard/routes/okf-route.mjs.map +1 -1
- package/dist/dashboard/routes/sitemap-ai-route.js +16 -4
- package/dist/dashboard/routes/sitemap-ai-route.js.map +1 -1
- package/dist/dashboard/routes/sitemap-ai-route.mjs +16 -4
- package/dist/dashboard/routes/sitemap-ai-route.mjs.map +1 -1
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,13 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
+
});
|
|
7
|
+
|
|
1
8
|
// src/dashboard/routes/markdown-route.ts
|
|
2
9
|
import { NextResponse } from "next/server";
|
|
3
|
-
import
|
|
10
|
+
import path4 from "path";
|
|
4
11
|
|
|
5
12
|
// src/core/mdx-reader.ts
|
|
6
13
|
import fs from "fs";
|
|
@@ -9,14 +16,26 @@ import matter from "gray-matter";
|
|
|
9
16
|
var MdxReader = class {
|
|
10
17
|
constructor(options) {
|
|
11
18
|
this.contentDir = options.contentDir;
|
|
19
|
+
this.stripSegments = options.stripSegments ?? [];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Remove configured URL-only segments from a slug so it maps to the file
|
|
23
|
+
* layout. e.g. stripSegments ['learn'] turns 'en/learn/hydroponics/x' into
|
|
24
|
+
* 'en/hydroponics/x'. Only whole path segments are removed.
|
|
25
|
+
*/
|
|
26
|
+
applyStrip(slug) {
|
|
27
|
+
if (this.stripSegments.length === 0) return slug;
|
|
28
|
+
const drop = new Set(this.stripSegments);
|
|
29
|
+
return slug.split("/").filter((seg) => !drop.has(seg)).join("/");
|
|
12
30
|
}
|
|
13
31
|
/** Read a single MDX file by slug. Returns null if not found. */
|
|
14
32
|
read(slug) {
|
|
33
|
+
const resolved = this.applyStrip(slug);
|
|
15
34
|
const candidates = [
|
|
16
|
-
path.join(this.contentDir, `${
|
|
17
|
-
path.join(this.contentDir, `${
|
|
18
|
-
path.join(this.contentDir,
|
|
19
|
-
path.join(this.contentDir,
|
|
35
|
+
path.join(this.contentDir, `${resolved}.mdx`),
|
|
36
|
+
path.join(this.contentDir, `${resolved}.md`),
|
|
37
|
+
path.join(this.contentDir, resolved, "index.mdx"),
|
|
38
|
+
path.join(this.contentDir, resolved, "index.md")
|
|
20
39
|
];
|
|
21
40
|
for (const filePath of candidates) {
|
|
22
41
|
if (fs.existsSync(filePath)) {
|
|
@@ -167,18 +186,193 @@ var CacheManager = class {
|
|
|
167
186
|
}
|
|
168
187
|
};
|
|
169
188
|
|
|
189
|
+
// src/analytics/visit-tracker.ts
|
|
190
|
+
import fs3 from "fs";
|
|
191
|
+
import path3 from "path";
|
|
192
|
+
|
|
193
|
+
// src/detection/known-patterns.ts
|
|
194
|
+
var KNOWN_BOTS = [
|
|
195
|
+
// AI Crawlers
|
|
196
|
+
{ name: "ClaudeBot", category: "ai_crawler", patterns: [/claudebot/i, /claude-web/i] },
|
|
197
|
+
{ name: "GPTBot", category: "ai_crawler", patterns: [/gptbot/i] },
|
|
198
|
+
{ name: "ChatGPT-User", category: "ai_crawler", patterns: [/chatgpt-user/i] },
|
|
199
|
+
{ name: "PerplexityBot", category: "ai_crawler", patterns: [/perplexitybot/i] },
|
|
200
|
+
{ name: "Googlebot-AI", category: "ai_crawler", patterns: [/google-extended/i, /googleother/i] },
|
|
201
|
+
{ name: "FacebookBot", category: "ai_crawler", patterns: [/facebookbot/i] },
|
|
202
|
+
{ name: "Applebot-Extended", category: "ai_crawler", patterns: [/applebot-extended/i] },
|
|
203
|
+
{ name: "YouBot", category: "ai_crawler", patterns: [/youbot/i] },
|
|
204
|
+
{ name: "CCBot", category: "ai_crawler", patterns: [/ccbot/i] },
|
|
205
|
+
{ name: "CohereCrawler", category: "ai_crawler", patterns: [/cohere-ai/i] },
|
|
206
|
+
{ name: "AI2Bot", category: "ai_crawler", patterns: [/ai2bot/i] },
|
|
207
|
+
{ name: "Bytespider", category: "ai_crawler", patterns: [/bytespider/i] },
|
|
208
|
+
{ name: "Diffbot", category: "ai_crawler", patterns: [/diffbot/i] },
|
|
209
|
+
// Search Engines
|
|
210
|
+
{ name: "Googlebot", category: "search_engine", patterns: [/googlebot/i] },
|
|
211
|
+
{ name: "Bingbot", category: "search_engine", patterns: [/bingbot/i, /msnbot/i] },
|
|
212
|
+
{ name: "DuckDuckBot", category: "search_engine", patterns: [/duckduckbot/i] },
|
|
213
|
+
{ name: "Baiduspider", category: "search_engine", patterns: [/baiduspider/i] },
|
|
214
|
+
{ name: "YandexBot", category: "search_engine", patterns: [/yandexbot/i] },
|
|
215
|
+
{ name: "Sogou", category: "search_engine", patterns: [/sogou/i] },
|
|
216
|
+
{ name: "Exabot", category: "search_engine", patterns: [/exabot/i] },
|
|
217
|
+
{ name: "ia_archiver", category: "search_engine", patterns: [/ia_archiver/i] }
|
|
218
|
+
];
|
|
219
|
+
|
|
220
|
+
// src/detection/bot-detection-pipeline.ts
|
|
221
|
+
function detectBot(input) {
|
|
222
|
+
const ua = input.userAgent ?? "";
|
|
223
|
+
for (const bot of KNOWN_BOTS) {
|
|
224
|
+
for (const pattern of bot.patterns) {
|
|
225
|
+
if (pattern.test(ua)) {
|
|
226
|
+
return {
|
|
227
|
+
isBot: true,
|
|
228
|
+
botName: bot.name,
|
|
229
|
+
confidence: "high",
|
|
230
|
+
detectionMethod: "known_pattern",
|
|
231
|
+
category: bot.category,
|
|
232
|
+
rawUserAgent: ua
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
const heuristicResult = checkHeuristics(ua, input.headers ?? {});
|
|
238
|
+
if (heuristicResult) return { ...heuristicResult, rawUserAgent: ua };
|
|
239
|
+
if (looksLikeBotUa(ua)) {
|
|
240
|
+
return {
|
|
241
|
+
isBot: true,
|
|
242
|
+
botName: null,
|
|
243
|
+
confidence: "low",
|
|
244
|
+
detectionMethod: "auto_learned",
|
|
245
|
+
category: "unknown_bot",
|
|
246
|
+
rawUserAgent: ua
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
isBot: false,
|
|
251
|
+
botName: null,
|
|
252
|
+
confidence: "high",
|
|
253
|
+
detectionMethod: "none",
|
|
254
|
+
category: "human",
|
|
255
|
+
rawUserAgent: ua
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
function checkHeuristics(ua, headers) {
|
|
259
|
+
if (/headlesschrome/i.test(ua)) {
|
|
260
|
+
return { isBot: true, botName: "HeadlessChrome", confidence: "medium", detectionMethod: "heuristic", category: "unknown_bot" };
|
|
261
|
+
}
|
|
262
|
+
if (/phantomjs/i.test(ua)) {
|
|
263
|
+
return { isBot: true, botName: "PhantomJS", confidence: "high", detectionMethod: "heuristic", category: "unknown_bot" };
|
|
264
|
+
}
|
|
265
|
+
if (/selenium/i.test(ua)) {
|
|
266
|
+
return { isBot: true, botName: "Selenium", confidence: "high", detectionMethod: "heuristic", category: "unknown_bot" };
|
|
267
|
+
}
|
|
268
|
+
if (ua.trim().length < 10) {
|
|
269
|
+
return { isBot: true, botName: null, confidence: "low", detectionMethod: "heuristic", category: "unknown_bot" };
|
|
270
|
+
}
|
|
271
|
+
const hasAcceptLang = !!headers["accept-language"];
|
|
272
|
+
const hasAcceptEncoding = !!headers["accept-encoding"];
|
|
273
|
+
const claimsBrowser = /chrome|firefox|safari|edge|opera|gecko|applewebkit/i.test(ua);
|
|
274
|
+
if (!hasAcceptLang && !hasAcceptEncoding && !claimsBrowser) {
|
|
275
|
+
return { isBot: true, botName: null, confidence: "low", detectionMethod: "heuristic", category: "unknown_bot" };
|
|
276
|
+
}
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
function looksLikeBotUa(ua) {
|
|
280
|
+
return /bot|crawler|spider|scraper|fetch|http|python|curl|java|ruby|go-http|node/i.test(ua) && !/chrome|firefox|safari|edge|opera/i.test(ua);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/analytics/geolocation.ts
|
|
284
|
+
var geoip = null;
|
|
285
|
+
function loadGeoip() {
|
|
286
|
+
if (geoip) return geoip;
|
|
287
|
+
try {
|
|
288
|
+
geoip = __require("geoip-lite");
|
|
289
|
+
} catch {
|
|
290
|
+
geoip = null;
|
|
291
|
+
}
|
|
292
|
+
return geoip;
|
|
293
|
+
}
|
|
294
|
+
function getCountry(ip) {
|
|
295
|
+
if (!ip || ip === "unknown" || ip === "127.0.0.1" || ip.startsWith("::")) return null;
|
|
296
|
+
const geo = loadGeoip();
|
|
297
|
+
if (!geo) return null;
|
|
298
|
+
try {
|
|
299
|
+
const result = geo.lookup(ip);
|
|
300
|
+
return result?.country ?? null;
|
|
301
|
+
} catch {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/analytics/visit-tracker.ts
|
|
307
|
+
var _VisitTracker = class _VisitTracker {
|
|
308
|
+
constructor(dataDir) {
|
|
309
|
+
this.dataDir = dataDir;
|
|
310
|
+
}
|
|
311
|
+
static getInstance(dataDir = process.env.TA_DATA_DIR ?? "data") {
|
|
312
|
+
if (!_VisitTracker.instance) {
|
|
313
|
+
_VisitTracker.instance = new _VisitTracker(dataDir);
|
|
314
|
+
}
|
|
315
|
+
return _VisitTracker.instance;
|
|
316
|
+
}
|
|
317
|
+
record(req, meta = {}) {
|
|
318
|
+
const ua = req.headers.get("user-agent") ?? "";
|
|
319
|
+
const headers = {};
|
|
320
|
+
req.headers.forEach((value, key) => {
|
|
321
|
+
headers[key] = value;
|
|
322
|
+
});
|
|
323
|
+
const result = detectBot({ userAgent: ua, headers });
|
|
324
|
+
if (!result.isBot) return;
|
|
325
|
+
const ip = req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? req.headers.get("x-real-ip") ?? "unknown";
|
|
326
|
+
const record = {
|
|
327
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
328
|
+
bot_name: result.botName,
|
|
329
|
+
bot_category: result.category,
|
|
330
|
+
detection_method: result.detectionMethod,
|
|
331
|
+
confidence: result.confidence,
|
|
332
|
+
url: req.nextUrl.pathname,
|
|
333
|
+
ip,
|
|
334
|
+
country: getCountry(ip),
|
|
335
|
+
user_agent: ua,
|
|
336
|
+
referer: req.headers.get("referer"),
|
|
337
|
+
response_ms: meta.responseMs ?? null,
|
|
338
|
+
cache_hit: meta.cacheHit ?? false,
|
|
339
|
+
content_length: meta.contentLength ?? null
|
|
340
|
+
};
|
|
341
|
+
this.append("ta-visits.jsonl", record);
|
|
342
|
+
}
|
|
343
|
+
append(filename, record) {
|
|
344
|
+
try {
|
|
345
|
+
const filePath = path3.join(this.dataDir, filename);
|
|
346
|
+
fs3.mkdirSync(this.dataDir, { recursive: true });
|
|
347
|
+
fs3.appendFileSync(filePath, JSON.stringify(record) + "\n", "utf-8");
|
|
348
|
+
} catch {
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
_VisitTracker.instance = null;
|
|
353
|
+
var VisitTracker = _VisitTracker;
|
|
354
|
+
|
|
170
355
|
// src/dashboard/routes/markdown-route.ts
|
|
171
|
-
var reader = new MdxReader({
|
|
356
|
+
var reader = new MdxReader({
|
|
357
|
+
contentDir: path4.join(process.cwd(), process.env.TA_CONTENT_DIR ?? "content"),
|
|
358
|
+
stripSegments: (process.env.TA_STRIP_SEGMENTS ?? "").split(",").map((s) => s.trim()).filter(Boolean)
|
|
359
|
+
});
|
|
172
360
|
var renderer = new MarkdownRenderer();
|
|
173
361
|
var cache = new CacheManager({
|
|
174
|
-
cacheDir:
|
|
362
|
+
cacheDir: path4.join(process.cwd(), process.env.TA_DATA_DIR ?? "data", "ta-cache")
|
|
175
363
|
});
|
|
176
364
|
async function GET(req, { params }) {
|
|
365
|
+
const startedAt = Date.now();
|
|
177
366
|
const { slug: slugParts } = await params;
|
|
178
367
|
const slug = slugParts.join("/");
|
|
179
368
|
const cacheKey = `markdown:${slug}`;
|
|
180
369
|
const cached = cache.get(cacheKey);
|
|
181
370
|
if (cached) {
|
|
371
|
+
VisitTracker.getInstance().record(req, {
|
|
372
|
+
responseMs: Date.now() - startedAt,
|
|
373
|
+
cacheHit: true,
|
|
374
|
+
contentLength: cached.length
|
|
375
|
+
});
|
|
182
376
|
return new NextResponse(cached, {
|
|
183
377
|
headers: {
|
|
184
378
|
"Content-Type": "text/markdown; charset=utf-8",
|
|
@@ -192,6 +386,11 @@ async function GET(req, { params }) {
|
|
|
192
386
|
}
|
|
193
387
|
const markdown = renderer.render(file);
|
|
194
388
|
cache.set(cacheKey, markdown);
|
|
389
|
+
VisitTracker.getInstance().record(req, {
|
|
390
|
+
responseMs: Date.now() - startedAt,
|
|
391
|
+
cacheHit: false,
|
|
392
|
+
contentLength: markdown.length
|
|
393
|
+
});
|
|
195
394
|
return new NextResponse(markdown, {
|
|
196
395
|
headers: {
|
|
197
396
|
"Content-Type": "text/markdown; charset=utf-8",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/dashboard/routes/markdown-route.ts","../../../src/core/mdx-reader.ts","../../../src/core/markdown-renderer.ts","../../../src/cache/cache-manager.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server'\nimport path from 'path'\nimport { MdxReader } from '../../core/mdx-reader.js'\nimport { MarkdownRenderer } from '../../core/markdown-renderer.js'\nimport { CacheManager } from '../../cache/cache-manager.js'\n\nconst reader = new MdxReader({ contentDir: path.join(process.cwd(), process.env.TA_CONTENT_DIR ?? 'content') })\nconst renderer = new MarkdownRenderer()\nconst cache = new CacheManager({\n cacheDir: path.join(process.cwd(), process.env.TA_DATA_DIR ?? 'data', 'ta-cache'),\n})\n\n/**\n * Handler for GET /api/third-audience/markdown/[...slug]\n *\n * Install in your Next.js app at:\n * app/api/third-audience/markdown/[...slug]/route.ts\n */\nexport async function GET(req: NextRequest, { params }: { params: Promise<{ slug: string[] }> }) {\n const { slug: slugParts } = await params\n const slug = slugParts.join('/')\n const cacheKey = `markdown:${slug}`\n\n const cached = cache.get(cacheKey)\n if (cached) {\n return new NextResponse(cached, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'X-Cache': 'HIT',\n },\n })\n }\n\n const file = reader.read(slug)\n if (!file) {\n return new NextResponse('Not Found', { status: 404 })\n }\n\n const markdown = renderer.render(file)\n cache.set(cacheKey, markdown)\n\n return new NextResponse(markdown, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'X-Cache': 'MISS',\n },\n })\n}\n","import fs from 'fs'\nimport path from 'path'\nimport matter from 'gray-matter'\n\nexport interface MdxFile {\n slug: string // relative path without extension, e.g. 'blog/my-post'\n filePath: string // absolute path to .mdx file\n frontmatter: Record<string, unknown>\n rawContent: string // body after frontmatter\n}\n\nexport interface MdxReaderOptions {\n contentDir: string // absolute path to content directory\n}\n\nexport class MdxReader {\n private contentDir: string\n\n constructor(options: MdxReaderOptions) {\n this.contentDir = options.contentDir\n }\n\n /** Read a single MDX file by slug. Returns null if not found. */\n read(slug: string): MdxFile | null {\n const candidates = [\n path.join(this.contentDir, `${slug}.mdx`),\n path.join(this.contentDir, `${slug}.md`),\n path.join(this.contentDir, slug, 'index.mdx'),\n path.join(this.contentDir, slug, 'index.md'),\n ]\n\n for (const filePath of candidates) {\n if (fs.existsSync(filePath)) {\n return this.parseFile(slug, filePath)\n }\n }\n\n return null\n }\n\n /** Read all MDX files recursively. */\n readAll(): MdxFile[] {\n if (!fs.existsSync(this.contentDir)) return []\n return this.walkDir(this.contentDir, this.contentDir)\n }\n\n private walkDir(dir: string, root: string): MdxFile[] {\n const results: MdxFile[] = []\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n const fullPath = path.join(dir, entry.name)\n if (entry.isDirectory()) {\n results.push(...this.walkDir(fullPath, root))\n } else if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {\n const relative = path.relative(root, fullPath)\n const slug = relative.replace(/\\.(mdx|md)$/, '').replace(/\\/index$/, '')\n results.push(this.parseFile(slug, fullPath))\n }\n }\n return results\n }\n\n private parseFile(slug: string, filePath: string): MdxFile {\n const raw = fs.readFileSync(filePath, 'utf-8')\n const { data: frontmatter, content: rawContent } = matter(raw)\n return { slug, filePath, frontmatter, rawContent }\n }\n}\n","import type { MdxFile } from './mdx-reader.js'\n\n/**\n * Strips JSX from MDX content and returns clean Markdown\n * suitable for AI crawlers.\n *\n * Removes:\n * - import/export statements\n * - JSX component tags (<ComponentName ... /> and <ComponentName>...</ComponentName>)\n * - Inline expressions {variable} that aren't standard Markdown\n *\n * Preserves:\n * - All standard Markdown (headings, lists, code blocks, links, images)\n * - Frontmatter (serialized as YAML header)\n */\nexport class MarkdownRenderer {\n render(file: MdxFile): string {\n const header = this.buildFrontmatterHeader(file.frontmatter)\n const body = this.stripJsx(file.rawContent)\n return header ? `${header}\\n\\n${body}` : body\n }\n\n private buildFrontmatterHeader(fm: Record<string, unknown>): string {\n const keys = Object.keys(fm)\n if (keys.length === 0) return ''\n const lines = keys\n .filter(k => fm[k] !== undefined && fm[k] !== null)\n .map(k => `${k}: ${this.yamlValue(fm[k])}`)\n return `---\\n${lines.join('\\n')}\\n---`\n }\n\n private yamlValue(val: unknown): string {\n if (typeof val === 'string') {\n // Quote strings containing special YAML chars\n return /[:#\\[\\]{},&*?|<>=!%@`]/.test(val) ? `\"${val.replace(/\"/g, '\\\\\"')}\"` : val\n }\n if (val instanceof Date) return val.toISOString()\n if (Array.isArray(val)) return `[${val.map(v => this.yamlValue(v)).join(', ')}]`\n return String(val)\n }\n\n private stripJsx(content: string): string {\n let out = content\n\n // Remove import statements: import Foo from '...' / import { Foo } from '...'\n out = out.replace(/^import\\s+.*?['\"].*?['\"]\\s*\\n?/gm, '')\n\n // Remove export statements at line start (export const, export default, export { })\n out = out.replace(/^export\\s+(?:default\\s+)?(?:const|let|var|function|class)\\s+[\\s\\S]*?(?=\\n(?=[^{]|\\n)|\\n{2,})/gm, '')\n out = out.replace(/^export\\s*\\{[^}]*\\}\\s*(?:from\\s+['\"][^'\"]*['\"])?\\s*\\n?/gm, '')\n\n // Remove self-closing JSX tags: <Component ... />\n // Must not match HTML img/br/hr which are valid Markdown\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*\\/>/g, '')\n\n // Remove JSX block tags: <Component ...>...</Component>\n // Greedy but bounded by matching closing tag\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*>[\\s\\S]*?<\\/\\1>/g, '')\n\n // Remove JSX expression blocks { expression } that span a whole line\n out = out.replace(/^\\s*\\{[^}]+\\}\\s*\\n/gm, '')\n\n // Collapse multiple blank lines to two\n out = out.replace(/\\n{3,}/g, '\\n\\n')\n\n return out.trim()\n }\n}\n","import fs from 'fs'\nimport path from 'path'\nimport crypto from 'crypto'\n\ninterface CacheEntry {\n content: string\n etag: string\n cachedAt: number\n ttl: number\n}\n\n/**\n * Two-tier cache:\n * 1. In-memory LRU (per Node.js process, instant)\n * 2. File-system cache in data/ta-cache/ (survives restarts)\n */\nexport class CacheManager {\n private memCache = new Map<string, CacheEntry>()\n private cacheDir: string\n private maxMemoryEntries: number\n private defaultTtl: number\n\n constructor(opts: { cacheDir: string; maxMemoryEntries?: number; ttl?: number }) {\n this.cacheDir = opts.cacheDir\n this.maxMemoryEntries = opts.maxMemoryEntries ?? 500\n this.defaultTtl = opts.ttl ?? 3600\n }\n\n get(key: string): string | null {\n // Check memory first\n const mem = this.memCache.get(key)\n if (mem && this.isValid(mem)) return mem.content\n if (mem) this.memCache.delete(key)\n\n // Check file cache\n const file = this.readFileCache(key)\n if (file && this.isValid(file)) {\n this.setMemory(key, file)\n return file.content\n }\n\n return null\n }\n\n set(key: string, content: string, etag = '', ttl = this.defaultTtl): void {\n const entry: CacheEntry = { content, etag, cachedAt: Date.now(), ttl }\n this.setMemory(key, entry)\n this.writeFileCache(key, entry)\n }\n\n /** Invalidate by key prefix — used when source .mdx file changes. */\n invalidate(keyPrefix: string): void {\n for (const k of this.memCache.keys()) {\n if (k.startsWith(keyPrefix)) this.memCache.delete(k)\n }\n const dir = this.cacheDir\n if (!fs.existsSync(dir)) return\n for (const file of fs.readdirSync(dir)) {\n if (file.startsWith(this.hashKey(keyPrefix).slice(0, 8))) {\n fs.unlinkSync(path.join(dir, file))\n }\n }\n }\n\n stats(): { memEntries: number; fsEntries: number } {\n const fsEntries = fs.existsSync(this.cacheDir)\n ? fs.readdirSync(this.cacheDir).filter(f => f.endsWith('.json')).length\n : 0\n return { memEntries: this.memCache.size, fsEntries }\n }\n\n private isValid(entry: CacheEntry): boolean {\n return Date.now() - entry.cachedAt < entry.ttl * 1000\n }\n\n private setMemory(key: string, entry: CacheEntry): void {\n if (this.memCache.size >= this.maxMemoryEntries) {\n // Evict oldest entry\n const firstKey = this.memCache.keys().next().value\n if (firstKey) this.memCache.delete(firstKey)\n }\n this.memCache.set(key, entry)\n }\n\n private hashKey(key: string): string {\n return crypto.createHash('sha256').update(key).digest('hex')\n }\n\n private filePath(key: string): string {\n return path.join(this.cacheDir, `${this.hashKey(key)}.json`)\n }\n\n private readFileCache(key: string): CacheEntry | null {\n const fp = this.filePath(key)\n if (!fs.existsSync(fp)) return null\n try {\n return JSON.parse(fs.readFileSync(fp, 'utf-8')) as CacheEntry\n } catch {\n return null\n }\n }\n\n private writeFileCache(key: string, entry: CacheEntry): void {\n try {\n fs.mkdirSync(this.cacheDir, { recursive: true })\n fs.writeFileSync(this.filePath(key), JSON.stringify(entry), 'utf-8')\n } catch {\n // Cache writes must never throw\n }\n }\n}\n"],"mappings":";AAAA,SAAS,oBAAsC;AAC/C,OAAOA,WAAU;;;ACDjB,OAAO,QAAQ;AACf,OAAO,UAAU;AACjB,OAAO,YAAY;AAaZ,IAAM,YAAN,MAAgB;AAAA,EAGrB,YAAY,SAA2B;AACrC,SAAK,aAAa,QAAQ;AAAA,EAC5B;AAAA;AAAA,EAGA,KAAK,MAA8B;AACjC,UAAM,aAAa;AAAA,MACjB,KAAK,KAAK,KAAK,YAAY,GAAG,IAAI,MAAM;AAAA,MACxC,KAAK,KAAK,KAAK,YAAY,GAAG,IAAI,KAAK;AAAA,MACvC,KAAK,KAAK,KAAK,YAAY,MAAM,WAAW;AAAA,MAC5C,KAAK,KAAK,KAAK,YAAY,MAAM,UAAU;AAAA,IAC7C;AAEA,eAAW,YAAY,YAAY;AACjC,UAAI,GAAG,WAAW,QAAQ,GAAG;AAC3B,eAAO,KAAK,UAAU,MAAM,QAAQ;AAAA,MACtC;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,UAAqB;AACnB,QAAI,CAAC,GAAG,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QAAM,OAAO,UAAU,KAAK,GAAG;AAC/B,QAAM,WAAW,YAAY,IAAI;AAEjC,QAAM,SAAS,MAAM,IAAI,QAAQ;AACjC,MAAI,QAAQ;AACV,WAAO,IAAI,aAAa,QAAQ;AAAA,MAC9B,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,OAAO,OAAO,KAAK,IAAI;AAC7B,MAAI,CAAC,MAAM;AACT,WAAO,IAAI,aAAa,aAAa,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtD;AAEA,QAAM,WAAW,SAAS,OAAO,IAAI;AACrC,QAAM,IAAI,UAAU,QAAQ;AAE5B,SAAO,IAAI,aAAa,UAAU;AAAA,IAChC,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,WAAW;AAAA,IACb;AAAA,EACF,CAAC;AACH;","names":["path","fs","path","path"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/dashboard/routes/markdown-route.ts","../../../src/core/mdx-reader.ts","../../../src/core/markdown-renderer.ts","../../../src/cache/cache-manager.ts","../../../src/analytics/visit-tracker.ts","../../../src/detection/known-patterns.ts","../../../src/detection/bot-detection-pipeline.ts","../../../src/analytics/geolocation.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server'\nimport path from 'path'\nimport { MdxReader } from '../../core/mdx-reader.js'\nimport { MarkdownRenderer } from '../../core/markdown-renderer.js'\nimport { CacheManager } from '../../cache/cache-manager.js'\nimport { VisitTracker } from '../../analytics/visit-tracker.js'\n\nconst reader = new MdxReader({\n contentDir: path.join(process.cwd(), process.env.TA_CONTENT_DIR ?? 'content'),\n stripSegments: (process.env.TA_STRIP_SEGMENTS ?? '').split(',').map((s) => s.trim()).filter(Boolean),\n})\nconst renderer = new MarkdownRenderer()\nconst cache = new CacheManager({\n cacheDir: path.join(process.cwd(), process.env.TA_DATA_DIR ?? 'data', 'ta-cache'),\n})\n\n/**\n * Handler for GET /api/third-audience/markdown/[...slug]\n *\n * Install in your Next.js app at:\n * app/api/third-audience/markdown/[...slug]/route.ts\n */\nexport async function GET(req: NextRequest, { params }: { params: Promise<{ slug: string[] }> }) {\n const startedAt = Date.now()\n const { slug: slugParts } = await params\n const slug = slugParts.join('/')\n const cacheKey = `markdown:${slug}`\n\n const cached = cache.get(cacheKey)\n if (cached) {\n // Record the bot visit (VisitTracker no-ops for non-bot user agents).\n VisitTracker.getInstance().record(req, {\n responseMs: Date.now() - startedAt,\n cacheHit: true,\n contentLength: cached.length,\n })\n return new NextResponse(cached, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'X-Cache': 'HIT',\n },\n })\n }\n\n const file = reader.read(slug)\n if (!file) {\n return new NextResponse('Not Found', { status: 404 })\n }\n\n const markdown = renderer.render(file)\n cache.set(cacheKey, markdown)\n\n // Record the bot visit (VisitTracker no-ops for non-bot user agents).\n VisitTracker.getInstance().record(req, {\n responseMs: Date.now() - startedAt,\n cacheHit: false,\n contentLength: markdown.length,\n })\n\n return new NextResponse(markdown, {\n headers: {\n 'Content-Type': 'text/markdown; charset=utf-8',\n 'X-Cache': 'MISS',\n },\n })\n}\n","import fs from 'fs'\nimport path from 'path'\nimport matter from 'gray-matter'\n\nexport interface MdxFile {\n slug: string // relative path without extension, e.g. 'blog/my-post'\n filePath: string // absolute path to .mdx file\n frontmatter: Record<string, unknown>\n rawContent: string // body after frontmatter\n}\n\nexport interface MdxReaderOptions {\n contentDir: string // absolute path to content directory\n /** URL path segments to drop when mapping a request slug to a file. */\n stripSegments?: string[]\n}\n\nexport class MdxReader {\n private contentDir: string\n private stripSegments: string[]\n\n constructor(options: MdxReaderOptions) {\n this.contentDir = options.contentDir\n this.stripSegments = options.stripSegments ?? []\n }\n\n /**\n * Remove configured URL-only segments from a slug so it maps to the file\n * layout. e.g. stripSegments ['learn'] turns 'en/learn/hydroponics/x' into\n * 'en/hydroponics/x'. Only whole path segments are removed.\n */\n private applyStrip(slug: string): string {\n if (this.stripSegments.length === 0) return slug\n const drop = new Set(this.stripSegments)\n return slug\n .split('/')\n .filter((seg) => !drop.has(seg))\n .join('/')\n }\n\n /** Read a single MDX file by slug. Returns null if not found. */\n read(slug: string): MdxFile | null {\n const resolved = this.applyStrip(slug)\n const candidates = [\n path.join(this.contentDir, `${resolved}.mdx`),\n path.join(this.contentDir, `${resolved}.md`),\n path.join(this.contentDir, resolved, 'index.mdx'),\n path.join(this.contentDir, resolved, 'index.md'),\n ]\n\n for (const filePath of candidates) {\n if (fs.existsSync(filePath)) {\n return this.parseFile(slug, filePath)\n }\n }\n\n return null\n }\n\n /** Read all MDX files recursively. */\n readAll(): MdxFile[] {\n if (!fs.existsSync(this.contentDir)) return []\n return this.walkDir(this.contentDir, this.contentDir)\n }\n\n private walkDir(dir: string, root: string): MdxFile[] {\n const results: MdxFile[] = []\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n const fullPath = path.join(dir, entry.name)\n if (entry.isDirectory()) {\n results.push(...this.walkDir(fullPath, root))\n } else if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {\n const relative = path.relative(root, fullPath)\n const slug = relative.replace(/\\.(mdx|md)$/, '').replace(/\\/index$/, '')\n results.push(this.parseFile(slug, fullPath))\n }\n }\n return results\n }\n\n private parseFile(slug: string, filePath: string): MdxFile {\n const raw = fs.readFileSync(filePath, 'utf-8')\n const { data: frontmatter, content: rawContent } = matter(raw)\n return { slug, filePath, frontmatter, rawContent }\n }\n}\n","import type { MdxFile } from './mdx-reader.js'\n\n/**\n * Strips JSX from MDX content and returns clean Markdown\n * suitable for AI crawlers.\n *\n * Removes:\n * - import/export statements\n * - JSX component tags (<ComponentName ... /> and <ComponentName>...</ComponentName>)\n * - Inline expressions {variable} that aren't standard Markdown\n *\n * Preserves:\n * - All standard Markdown (headings, lists, code blocks, links, images)\n * - Frontmatter (serialized as YAML header)\n */\nexport class MarkdownRenderer {\n render(file: MdxFile): string {\n const header = this.buildFrontmatterHeader(file.frontmatter)\n const body = this.stripJsx(file.rawContent)\n return header ? `${header}\\n\\n${body}` : body\n }\n\n private buildFrontmatterHeader(fm: Record<string, unknown>): string {\n const keys = Object.keys(fm)\n if (keys.length === 0) return ''\n const lines = keys\n .filter(k => fm[k] !== undefined && fm[k] !== null)\n .map(k => `${k}: ${this.yamlValue(fm[k])}`)\n return `---\\n${lines.join('\\n')}\\n---`\n }\n\n private yamlValue(val: unknown): string {\n if (typeof val === 'string') {\n // Quote strings containing special YAML chars\n return /[:#\\[\\]{},&*?|<>=!%@`]/.test(val) ? `\"${val.replace(/\"/g, '\\\\\"')}\"` : val\n }\n if (val instanceof Date) return val.toISOString()\n if (Array.isArray(val)) return `[${val.map(v => this.yamlValue(v)).join(', ')}]`\n return String(val)\n }\n\n private stripJsx(content: string): string {\n let out = content\n\n // Remove import statements: import Foo from '...' / import { Foo } from '...'\n out = out.replace(/^import\\s+.*?['\"].*?['\"]\\s*\\n?/gm, '')\n\n // Remove export statements at line start (export const, export default, export { })\n out = out.replace(/^export\\s+(?:default\\s+)?(?:const|let|var|function|class)\\s+[\\s\\S]*?(?=\\n(?=[^{]|\\n)|\\n{2,})/gm, '')\n out = out.replace(/^export\\s*\\{[^}]*\\}\\s*(?:from\\s+['\"][^'\"]*['\"])?\\s*\\n?/gm, '')\n\n // Remove self-closing JSX tags: <Component ... />\n // Must not match HTML img/br/hr which are valid Markdown\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*\\/>/g, '')\n\n // Remove JSX block tags: <Component ...>...</Component>\n // Greedy but bounded by matching closing tag\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*>[\\s\\S]*?<\\/\\1>/g, '')\n\n // Remove JSX expression blocks { expression } that span a whole line\n out = out.replace(/^\\s*\\{[^}]+\\}\\s*\\n/gm, '')\n\n // Collapse multiple blank lines to two\n out = out.replace(/\\n{3,}/g, '\\n\\n')\n\n return out.trim()\n }\n}\n","import fs from 'fs'\nimport path from 'path'\nimport crypto from 'crypto'\n\ninterface CacheEntry {\n content: string\n etag: string\n cachedAt: number\n ttl: number\n}\n\n/**\n * Two-tier cache:\n * 1. In-memory LRU (per Node.js process, instant)\n * 2. File-system cache in data/ta-cache/ (survives restarts)\n */\nexport class CacheManager {\n private memCache = new Map<string, CacheEntry>()\n private cacheDir: string\n private maxMemoryEntries: number\n private defaultTtl: number\n\n constructor(opts: { cacheDir: string; maxMemoryEntries?: number; ttl?: number }) {\n this.cacheDir = opts.cacheDir\n this.maxMemoryEntries = opts.maxMemoryEntries ?? 500\n this.defaultTtl = opts.ttl ?? 3600\n }\n\n get(key: string): string | null {\n // Check memory first\n const mem = this.memCache.get(key)\n if (mem && this.isValid(mem)) return mem.content\n if (mem) this.memCache.delete(key)\n\n // Check file cache\n const file = this.readFileCache(key)\n if (file && this.isValid(file)) {\n this.setMemory(key, file)\n return file.content\n }\n\n return null\n }\n\n set(key: string, content: string, etag = '', ttl = this.defaultTtl): void {\n const entry: CacheEntry = { content, etag, cachedAt: Date.now(), ttl }\n this.setMemory(key, entry)\n this.writeFileCache(key, entry)\n }\n\n /** Invalidate by key prefix — used when source .mdx file changes. */\n invalidate(keyPrefix: string): void {\n for (const k of this.memCache.keys()) {\n if (k.startsWith(keyPrefix)) this.memCache.delete(k)\n }\n const dir = this.cacheDir\n if (!fs.existsSync(dir)) return\n for (const file of fs.readdirSync(dir)) {\n if (file.startsWith(this.hashKey(keyPrefix).slice(0, 8))) {\n fs.unlinkSync(path.join(dir, file))\n }\n }\n }\n\n stats(): { memEntries: number; fsEntries: number } {\n const fsEntries = fs.existsSync(this.cacheDir)\n ? fs.readdirSync(this.cacheDir).filter(f => f.endsWith('.json')).length\n : 0\n return { memEntries: this.memCache.size, fsEntries }\n }\n\n private isValid(entry: CacheEntry): boolean {\n return Date.now() - entry.cachedAt < entry.ttl * 1000\n }\n\n private setMemory(key: string, entry: CacheEntry): void {\n if (this.memCache.size >= this.maxMemoryEntries) {\n // Evict oldest entry\n const firstKey = this.memCache.keys().next().value\n if (firstKey) this.memCache.delete(firstKey)\n }\n this.memCache.set(key, entry)\n }\n\n private hashKey(key: string): string {\n return crypto.createHash('sha256').update(key).digest('hex')\n }\n\n private filePath(key: string): string {\n return path.join(this.cacheDir, `${this.hashKey(key)}.json`)\n }\n\n private readFileCache(key: string): CacheEntry | null {\n const fp = this.filePath(key)\n if (!fs.existsSync(fp)) return null\n try {\n return JSON.parse(fs.readFileSync(fp, 'utf-8')) as CacheEntry\n } catch {\n return null\n }\n }\n\n private writeFileCache(key: string, entry: CacheEntry): void {\n try {\n fs.mkdirSync(this.cacheDir, { recursive: true })\n fs.writeFileSync(this.filePath(key), JSON.stringify(entry), 'utf-8')\n } catch {\n // Cache writes must never throw\n }\n }\n}\n","import fs from 'fs'\nimport path from 'path'\nimport type { NextRequest } from 'next/server'\nimport { detectBot } from '../detection/bot-detection-pipeline.js'\nimport { getCountry } from './geolocation.js'\n\nexport interface VisitRecord {\n timestamp: string\n bot_name: string | null\n bot_category: string\n detection_method: string\n confidence: string\n url: string\n ip: string\n country: string | null\n user_agent: string\n referer: string | null\n response_ms: number | null\n cache_hit: boolean\n content_length: number | null\n}\n\nexport class VisitTracker {\n private static instance: VisitTracker | null = null\n private dataDir: string\n\n private constructor(dataDir: string) {\n this.dataDir = dataDir\n }\n\n static getInstance(dataDir = process.env.TA_DATA_DIR ?? 'data'): VisitTracker {\n if (!VisitTracker.instance) {\n VisitTracker.instance = new VisitTracker(dataDir)\n }\n return VisitTracker.instance\n }\n\n record(req: NextRequest, meta: { responseMs?: number; cacheHit?: boolean; contentLength?: number } = {}): void {\n const ua = req.headers.get('user-agent') ?? ''\n const headers: Record<string, string> = {}\n req.headers.forEach((value, key) => { headers[key] = value })\n const result = detectBot({ userAgent: ua, headers })\n\n if (!result.isBot) return // only track bots\n\n const ip = req.headers.get('x-forwarded-for')?.split(',')[0]?.trim()\n ?? req.headers.get('x-real-ip')\n ?? 'unknown'\n\n const record: VisitRecord = {\n timestamp: new Date().toISOString(),\n bot_name: result.botName,\n bot_category: result.category,\n detection_method: result.detectionMethod,\n confidence: result.confidence,\n url: req.nextUrl.pathname,\n ip,\n country: getCountry(ip),\n user_agent: ua,\n referer: req.headers.get('referer'),\n response_ms: meta.responseMs ?? null,\n cache_hit: meta.cacheHit ?? false,\n content_length: meta.contentLength ?? null,\n }\n\n this.append('ta-visits.jsonl', record)\n }\n\n private append(filename: string, record: VisitRecord): void {\n try {\n const filePath = path.join(this.dataDir, filename)\n fs.mkdirSync(this.dataDir, { recursive: true })\n fs.appendFileSync(filePath, JSON.stringify(record) + '\\n', 'utf-8')\n } catch {\n // Tracking must never throw\n }\n }\n}\n","/** Known AI crawler and search engine user-agent patterns. */\nexport interface KnownBot {\n name: string\n category: 'ai_crawler' | 'search_engine'\n patterns: RegExp[]\n}\n\nexport const KNOWN_BOTS: KnownBot[] = [\n // AI Crawlers\n { name: 'ClaudeBot', category: 'ai_crawler', patterns: [/claudebot/i, /claude-web/i] },\n { name: 'GPTBot', category: 'ai_crawler', patterns: [/gptbot/i] },\n { name: 'ChatGPT-User', category: 'ai_crawler', patterns: [/chatgpt-user/i] },\n { name: 'PerplexityBot', category: 'ai_crawler', patterns: [/perplexitybot/i] },\n { name: 'Googlebot-AI', category: 'ai_crawler', patterns: [/google-extended/i, /googleother/i] },\n { name: 'FacebookBot', category: 'ai_crawler', patterns: [/facebookbot/i] },\n { name: 'Applebot-Extended',category: 'ai_crawler', patterns: [/applebot-extended/i] },\n { name: 'YouBot', category: 'ai_crawler', patterns: [/youbot/i] },\n { name: 'CCBot', category: 'ai_crawler', patterns: [/ccbot/i] },\n { name: 'CohereCrawler', category: 'ai_crawler', patterns: [/cohere-ai/i] },\n { name: 'AI2Bot', category: 'ai_crawler', patterns: [/ai2bot/i] },\n { name: 'Bytespider', category: 'ai_crawler', patterns: [/bytespider/i] },\n { name: 'Diffbot', category: 'ai_crawler', patterns: [/diffbot/i] },\n\n // Search Engines\n { name: 'Googlebot', category: 'search_engine', patterns: [/googlebot/i] },\n { name: 'Bingbot', category: 'search_engine', patterns: [/bingbot/i, /msnbot/i] },\n { name: 'DuckDuckBot', category: 'search_engine', patterns: [/duckduckbot/i] },\n { name: 'Baiduspider', category: 'search_engine', patterns: [/baiduspider/i] },\n { name: 'YandexBot', category: 'search_engine', patterns: [/yandexbot/i] },\n { name: 'Sogou', category: 'search_engine', patterns: [/sogou/i] },\n { name: 'Exabot', category: 'search_engine', patterns: [/exabot/i] },\n { name: 'ia_archiver', category: 'search_engine', patterns: [/ia_archiver/i] },\n]\n","import type { BotDetectionResult } from './bot-detection-result.js'\nimport { KNOWN_BOTS } from './known-patterns.js'\n\nexport interface DetectBotInput {\n userAgent: string\n /** Optional: headers map for heuristic checks */\n headers?: Record<string, string | string[] | undefined>\n /** Optional: IP address */\n ip?: string\n}\n\n/**\n * Three-layer bot detection pipeline:\n * 1. Known pattern matching (O(n) UA string match)\n * 2. Heuristic signals (missing headers, headless indicators)\n * 3. Auto-learner flag (unknown UAs that behave bot-like)\n */\nexport function detectBot(input: DetectBotInput): BotDetectionResult {\n const ua = input.userAgent ?? ''\n\n // Layer 1: known pattern match\n for (const bot of KNOWN_BOTS) {\n for (const pattern of bot.patterns) {\n if (pattern.test(ua)) {\n return {\n isBot: true,\n botName: bot.name,\n confidence: 'high',\n detectionMethod: 'known_pattern',\n category: bot.category,\n rawUserAgent: ua,\n }\n }\n }\n }\n\n // Layer 2: heuristics\n const heuristicResult = checkHeuristics(ua, input.headers ?? {})\n if (heuristicResult) return { ...heuristicResult, rawUserAgent: ua }\n\n // Layer 3: auto-learner — flag suspicious unknown UAs for review\n if (looksLikeBotUa(ua)) {\n return {\n isBot: true,\n botName: null,\n confidence: 'low',\n detectionMethod: 'auto_learned',\n category: 'unknown_bot',\n rawUserAgent: ua,\n }\n }\n\n return {\n isBot: false,\n botName: null,\n confidence: 'high',\n detectionMethod: 'none',\n category: 'human',\n rawUserAgent: ua,\n }\n}\n\nfunction checkHeuristics(\n ua: string,\n headers: Record<string, string | string[] | undefined>\n): Omit<BotDetectionResult, 'rawUserAgent'> | null {\n // Headless Chrome signals\n if (/headlesschrome/i.test(ua)) {\n return { isBot: true, botName: 'HeadlessChrome', confidence: 'medium', detectionMethod: 'heuristic', category: 'unknown_bot' }\n }\n if (/phantomjs/i.test(ua)) {\n return { isBot: true, botName: 'PhantomJS', confidence: 'high', detectionMethod: 'heuristic', category: 'unknown_bot' }\n }\n if (/selenium/i.test(ua)) {\n return { isBot: true, botName: 'Selenium', confidence: 'high', detectionMethod: 'heuristic', category: 'unknown_bot' }\n }\n\n // Empty or very short UA is suspicious\n if (ua.trim().length < 10) {\n return { isBot: true, botName: null, confidence: 'low', detectionMethod: 'heuristic', category: 'unknown_bot' }\n }\n\n // Missing typical browser headers — only a bot signal when the UA does NOT\n // present itself as a real browser. Genuine browsers occasionally arrive\n // without accept-language/accept-encoding (privacy extensions, proxies,\n // some CDNs), so we must not flag them on missing headers alone.\n const hasAcceptLang = !!headers['accept-language']\n const hasAcceptEncoding = !!headers['accept-encoding']\n const claimsBrowser = /chrome|firefox|safari|edge|opera|gecko|applewebkit/i.test(ua)\n if (!hasAcceptLang && !hasAcceptEncoding && !claimsBrowser) {\n return { isBot: true, botName: null, confidence: 'low', detectionMethod: 'heuristic', category: 'unknown_bot' }\n }\n\n return null\n}\n\nfunction looksLikeBotUa(ua: string): boolean {\n return (\n /bot|crawler|spider|scraper|fetch|http|python|curl|java|ruby|go-http|node/i.test(ua) &&\n !/chrome|firefox|safari|edge|opera/i.test(ua)\n )\n}\n","let geoip: typeof import('geoip-lite') | null = null\n\nfunction loadGeoip() {\n if (geoip) return geoip\n try {\n geoip = require('geoip-lite') as typeof import('geoip-lite')\n } catch {\n geoip = null\n }\n return geoip\n}\n\n/** Returns ISO 3166-1 alpha-2 country code, or null if lookup fails. */\nexport function getCountry(ip: string): string | null {\n if (!ip || ip === 'unknown' || ip === '127.0.0.1' || ip.startsWith('::')) return null\n const geo = loadGeoip()\n if (!geo) return null\n try {\n const result = geo.lookup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7B,UAAUA,MAAK,KAAK,QAAQ,IAAI,GAAG,QAAQ,IAAI,eAAe,QAAQ,UAAU;AAClF,CAAC;AAQD,eAAsB,IAAI,KAAkB,EAAE,OAAO,GAA4C;AAC/F,QAAM,YAAY,KAAK,IAAI;AAC3B,QAAM,EAAE,MAAM,UAAU,IAAI,MAAM;AAClC,QAAM,OAAO,UAAU,KAAK,GAAG;AAC/B,QAAM,WAAW,YAAY,IAAI;AAEjC,QAAM,SAAS,MAAM,IAAI,QAAQ;AACjC,MAAI,QAAQ;AAEV,iBAAa,YAAY,EAAE,OAAO,KAAK;AAAA,MACrC,YAAY,KAAK,IAAI,IAAI;AAAA,MACzB,UAAU;AAAA,MACV,eAAe,OAAO;AAAA,IACxB,CAAC;AACD,WAAO,IAAI,aAAa,QAAQ;AAAA,MAC9B,SAAS;AAAA,QACP,gBAAgB;AAAA,QAChB,WAAW;AAAA,MACb;AAAA,IACF,CAAC;AAAA,EACH;AAEA,QAAM,OAAO,OAAO,KAAK,IAAI;AAC7B,MAAI,CAAC,MAAM;AACT,WAAO,IAAI,aAAa,aAAa,EAAE,QAAQ,IAAI,CAAC;AAAA,EACtD;AAEA,QAAM,WAAW,SAAS,OAAO,IAAI;AACrC,QAAM,IAAI,UAAU,QAAQ;AAG5B,eAAa,YAAY,EAAE,OAAO,KAAK;AAAA,IACrC,YAAY,KAAK,IAAI,IAAI;AAAA,IACzB,UAAU;AAAA,IACV,eAAe,SAAS;AAAA,EAC1B,CAAC;AAED,SAAO,IAAI,aAAa,UAAU;AAAA,IAChC,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,WAAW;AAAA,IACb;AAAA,EACF,CAAC;AACH;","names":["path","fs","path","fs","path","path","fs","path"]}
|
|
@@ -43,14 +43,26 @@ var import_gray_matter = __toESM(require("gray-matter"));
|
|
|
43
43
|
var MdxReader = class {
|
|
44
44
|
constructor(options) {
|
|
45
45
|
this.contentDir = options.contentDir;
|
|
46
|
+
this.stripSegments = options.stripSegments ?? [];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Remove configured URL-only segments from a slug so it maps to the file
|
|
50
|
+
* layout. e.g. stripSegments ['learn'] turns 'en/learn/hydroponics/x' into
|
|
51
|
+
* 'en/hydroponics/x'. Only whole path segments are removed.
|
|
52
|
+
*/
|
|
53
|
+
applyStrip(slug) {
|
|
54
|
+
if (this.stripSegments.length === 0) return slug;
|
|
55
|
+
const drop = new Set(this.stripSegments);
|
|
56
|
+
return slug.split("/").filter((seg) => !drop.has(seg)).join("/");
|
|
46
57
|
}
|
|
47
58
|
/** Read a single MDX file by slug. Returns null if not found. */
|
|
48
59
|
read(slug) {
|
|
60
|
+
const resolved = this.applyStrip(slug);
|
|
49
61
|
const candidates = [
|
|
50
|
-
import_path.default.join(this.contentDir, `${
|
|
51
|
-
import_path.default.join(this.contentDir, `${
|
|
52
|
-
import_path.default.join(this.contentDir,
|
|
53
|
-
import_path.default.join(this.contentDir,
|
|
62
|
+
import_path.default.join(this.contentDir, `${resolved}.mdx`),
|
|
63
|
+
import_path.default.join(this.contentDir, `${resolved}.md`),
|
|
64
|
+
import_path.default.join(this.contentDir, resolved, "index.mdx"),
|
|
65
|
+
import_path.default.join(this.contentDir, resolved, "index.md")
|
|
54
66
|
];
|
|
55
67
|
for (const filePath of candidates) {
|
|
56
68
|
if (import_fs.default.existsSync(filePath)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/dashboard/routes/okf-graph-route.ts","../../../src/core/mdx-reader.ts","../../../src/core/markdown-renderer.ts","../../../src/okf/okf-bundle.ts","../../../src/dashboard/auth.ts","../../../src/dashboard/admin-store.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server'\nimport path from 'path'\nimport { MdxReader } from '../../core/mdx-reader.js'\nimport { buildOkfGraph } from '../../okf/okf-bundle.js'\nimport { checkApiAuth, unauthorizedResponse } from '../auth.js'\n\nconst reader = new MdxReader({ contentDir: path.join(process.cwd(), process.env.TA_CONTENT_DIR ?? 'content') })\n\n/** GET /api/third-audience/okf-graph — returns graph JSON for the OKF dashboard viewer */\nexport async function GET(req: NextRequest): Promise<NextResponse> {\n if (!checkApiAuth(req)) return unauthorizedResponse()\n\n const baseUrl = process.env.NEXT_PUBLIC_SITE_URL\n ?? `${req.nextUrl.protocol}//${req.nextUrl.host}`\n\n const files = reader.readAll()\n const graph = buildOkfGraph(files, baseUrl)\n\n return NextResponse.json({\n graph,\n stats: {\n pages: files.length,\n nodes: graph.nodes.length,\n edges: graph.edges.length,\n },\n indexUrl: `${baseUrl}/okf`,\n })\n}\n","import fs from 'fs'\nimport path from 'path'\nimport matter from 'gray-matter'\n\nexport interface MdxFile {\n slug: string // relative path without extension, e.g. 'blog/my-post'\n filePath: string // absolute path to .mdx file\n frontmatter: Record<string, unknown>\n rawContent: string // body after frontmatter\n}\n\nexport interface MdxReaderOptions {\n contentDir: string // absolute path to content directory\n}\n\nexport class MdxReader {\n private contentDir: string\n\n constructor(options: MdxReaderOptions) {\n this.contentDir = options.contentDir\n }\n\n /** Read a single MDX file by slug. Returns null if not found. */\n read(slug: string): MdxFile | null {\n const candidates = [\n path.join(this.contentDir, `${slug}.mdx`),\n path.join(this.contentDir, `${slug}.md`),\n path.join(this.contentDir, slug, 'index.mdx'),\n path.join(this.contentDir, slug, 'index.md'),\n ]\n\n for (const filePath of candidates) {\n if (fs.existsSync(filePath)) {\n return this.parseFile(slug, filePath)\n }\n }\n\n return null\n }\n\n /** Read all MDX files recursively. */\n readAll(): MdxFile[] {\n if (!fs.existsSync(this.contentDir)) return []\n return this.walkDir(this.contentDir, this.contentDir)\n }\n\n private walkDir(dir: string, root: string): MdxFile[] {\n const results: MdxFile[] = []\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n const fullPath = path.join(dir, entry.name)\n if (entry.isDirectory()) {\n results.push(...this.walkDir(fullPath, root))\n } else if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {\n const relative = path.relative(root, fullPath)\n const slug = relative.replace(/\\.(mdx|md)$/, '').replace(/\\/index$/, '')\n results.push(this.parseFile(slug, fullPath))\n }\n }\n return results\n }\n\n private parseFile(slug: string, filePath: string): MdxFile {\n const raw = fs.readFileSync(filePath, 'utf-8')\n const { data: frontmatter, content: rawContent } = matter(raw)\n return { slug, filePath, frontmatter, rawContent }\n }\n}\n","import type { MdxFile } from './mdx-reader.js'\n\n/**\n * Strips JSX from MDX content and returns clean Markdown\n * suitable for AI crawlers.\n *\n * Removes:\n * - import/export statements\n * - JSX component tags (<ComponentName ... /> and <ComponentName>...</ComponentName>)\n * - Inline expressions {variable} that aren't standard Markdown\n *\n * Preserves:\n * - All standard Markdown (headings, lists, code blocks, links, images)\n * - Frontmatter (serialized as YAML header)\n */\nexport class MarkdownRenderer {\n render(file: MdxFile): string {\n const header = this.buildFrontmatterHeader(file.frontmatter)\n const body = this.stripJsx(file.rawContent)\n return header ? `${header}\\n\\n${body}` : body\n }\n\n private buildFrontmatterHeader(fm: Record<string, unknown>): string {\n const keys = Object.keys(fm)\n if (keys.length === 0) return ''\n const lines = keys\n .filter(k => fm[k] !== undefined && fm[k] !== null)\n .map(k => `${k}: ${this.yamlValue(fm[k])}`)\n return `---\\n${lines.join('\\n')}\\n---`\n }\n\n private yamlValue(val: unknown): string {\n if (typeof val === 'string') {\n // Quote strings containing special YAML chars\n return /[:#\\[\\]{},&*?|<>=!%@`]/.test(val) ? `\"${val.replace(/\"/g, '\\\\\"')}\"` : val\n }\n if (val instanceof Date) return val.toISOString()\n if (Array.isArray(val)) return `[${val.map(v => this.yamlValue(v)).join(', ')}]`\n return String(val)\n }\n\n private stripJsx(content: string): string {\n let out = content\n\n // Remove import statements: import Foo from '...' / import { Foo } from '...'\n out = out.replace(/^import\\s+.*?['\"].*?['\"]\\s*\\n?/gm, '')\n\n // Remove export statements at line start (export const, export default, export { })\n out = out.replace(/^export\\s+(?:default\\s+)?(?:const|let|var|function|class)\\s+[\\s\\S]*?(?=\\n(?=[^{]|\\n)|\\n{2,})/gm, '')\n out = out.replace(/^export\\s*\\{[^}]*\\}\\s*(?:from\\s+['\"][^'\"]*['\"])?\\s*\\n?/gm, '')\n\n // Remove self-closing JSX tags: <Component ... />\n // Must not match HTML img/br/hr which are valid Markdown\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*\\/>/g, '')\n\n // Remove JSX block tags: <Component ...>...</Component>\n // Greedy but bounded by matching closing tag\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*>[\\s\\S]*?<\\/\\1>/g, '')\n\n // Remove JSX expression blocks { expression } that span a whole line\n out = out.replace(/^\\s*\\{[^}]+\\}\\s*\\n/gm, '')\n\n // Collapse multiple blank lines to two\n out = out.replace(/\\n{3,}/g, '\\n\\n')\n\n return out.trim()\n }\n}\n","import type { MdxFile } from '../core/mdx-reader.js'\nimport { MarkdownRenderer } from '../core/markdown-renderer.js'\n\nconst renderer = new MarkdownRenderer()\n\nexport interface OkfGraphNode {\n id: string\n title: string\n type: string\n url: string\n}\n\nexport interface OkfGraphEdge {\n source: string\n target: string\n}\n\nexport interface OkfGraphData {\n nodes: OkfGraphNode[]\n edges: OkfGraphEdge[]\n}\n\n/**\n * Builds the knowledge graph data for the OKF viewer.\n * Nodes = content pages; edges = internal links between them.\n * Trims to top 100 most-connected nodes (matching WP plugin behaviour).\n */\nexport function buildOkfGraph(files: MdxFile[], baseUrl: string): OkfGraphData {\n const base = baseUrl.replace(/\\/$/, '')\n const slugSet = new Set(files.map(f => f.slug))\n\n // Build slug → markdown map for link extraction\n const mdMap = new Map<string, string>()\n for (const file of files) {\n mdMap.set(file.slug, renderer.render(file))\n }\n\n // Count degrees to pick top 100\n const degrees = new Map<string, number>(files.map(f => [f.slug, 0]))\n const rawEdges: OkfGraphEdge[] = []\n\n for (const file of files) {\n const md = mdMap.get(file.slug) ?? ''\n const linkRe = /\\[([^\\]]+)\\]\\((\\/[^)]+)\\)/g\n let m: RegExpExecArray | null\n while ((m = linkRe.exec(md)) !== null) {\n const candidate = m[2].replace(/^\\//, '').replace(/\\.md$/, '')\n if (slugSet.has(candidate) && candidate !== file.slug) {\n rawEdges.push({ source: file.slug, target: candidate })\n degrees.set(file.slug, (degrees.get(file.slug) ?? 0) + 1)\n degrees.set(candidate, (degrees.get(candidate) ?? 0) + 1)\n }\n }\n }\n\n // Top 100 nodes by degree\n const top100 = files\n .slice()\n .sort((a, b) => (degrees.get(b.slug) ?? 0) - (degrees.get(a.slug) ?? 0))\n .slice(0, 100)\n const topSet = new Set(top100.map(f => f.slug))\n\n const nodes: OkfGraphNode[] = top100.map(f => ({\n id: f.slug,\n title: String(f.frontmatter.title ?? f.slug),\n type: String(f.frontmatter.type ?? 'WebPage'),\n url: `${base}/${f.slug}`,\n }))\n\n const edges = rawEdges.filter(e => topSet.has(e.source) && topSet.has(e.target))\n\n return { nodes, edges }\n}\n\n/** Generates the /okf/index.md manifest listing all content. */\nexport function generateOkfIndex(files: MdxFile[], baseUrl: string): string {\n const base = baseUrl.replace(/\\/$/, '')\n const lines = [\n '# Open Knowledge Format (OKF) Bundle',\n '',\n 'This bundle contains all content as clean Markdown files for AI consumption.',\n '',\n '## Contents',\n '',\n ]\n\n for (const file of files) {\n const fm = file.frontmatter\n const title = String(fm.title ?? file.slug)\n const desc = fm.description ? ` — ${String(fm.description)}` : ''\n lines.push(`- [${title}](${base}/okf/${file.slug}.md)${desc}`)\n }\n\n return lines.join('\\n') + '\\n'\n}\n\n/** Renders a single MDX file for OKF, with internal links rewritten to .md siblings. */\nexport function generateOkfPage(file: MdxFile, allFiles: MdxFile[], baseUrl: string): string {\n const markdown = renderer.render(file)\n return rewriteInternalLinks(markdown, allFiles, baseUrl)\n}\n\n/**\n * Rewrites internal links to point at sibling .md files in the OKF bundle.\n * e.g. [link](/blog/post) → [link](/okf/blog/post.md)\n */\nfunction rewriteInternalLinks(markdown: string, allFiles: MdxFile[], baseUrl: string): string {\n const slugSet = new Set(allFiles.map(f => f.slug))\n const base = baseUrl.replace(/\\/$/, '')\n\n return markdown.replace(/\\[([^\\]]+)\\]\\((\\/[^)]+)\\)/g, (match, text, href) => {\n // Strip leading slash and any trailing .md to get candidate slug\n const candidate = href.replace(/^\\//, '').replace(/\\.md$/, '')\n if (slugSet.has(candidate)) {\n return `[${text}](${base}/okf/${candidate}.md)`\n }\n return match\n })\n}\n","import type { NextRequest } from 'next/server'\nimport { NextResponse } from 'next/server'\nimport { verifySession, verifyApiKey } from './admin-store.js'\n\nconst SESSION_COOKIE = 'ta_session'\n\n/**\n * Authenticate an API route request. Accepts (in order):\n * 1. X-TA-Api-Key header — for headless/external callers (mirrors WP's approach)\n * 2. Authorization: Bearer <api-key> — same key, different transport\n * 3. Valid ta_session cookie — browser dashboard session\n */\nexport function checkApiAuth(req: NextRequest): boolean {\n // 1. X-TA-Api-Key header (WP-style headless key)\n const apiKeyHeader = req.headers.get('x-ta-api-key')\n if (apiKeyHeader) return verifyApiKey(apiKeyHeader)\n\n // 2. Bearer token (treat as api key)\n const auth = req.headers.get('authorization') ?? ''\n if (auth.startsWith('Bearer ')) {\n const token = auth.slice(7)\n return verifyApiKey(token)\n }\n\n // 3. Browser session cookie\n const session = req.cookies.get(SESSION_COOKIE)?.value\n if (session) return verifySession(session)\n\n return false\n}\n\n/**\n * Returns a 401 JSON response with the correct WWW-Authenticate header.\n * Use as: if (!checkApiAuth(req)) return unauthorizedResponse()\n */\nexport function unauthorizedResponse(): NextResponse {\n return NextResponse.json(\n { error: 'Unauthorized. Provide X-TA-Api-Key header or a valid session cookie.' },\n {\n status: 401,\n headers: { 'WWW-Authenticate': 'Bearer realm=\"Third Audience API\"' },\n }\n )\n}\n","import fs from 'fs'\nimport path from 'path'\nimport crypto from 'crypto'\n\nexport interface AdminRecord {\n passwordHash: string // sha256(secret + password)\n isDefaultPassword: boolean\n createdAt: string\n lastLoginAt: string | null\n apiKey?: string // AES-256-GCM encrypted, for headless/external API callers\n}\n\nfunction adminFilePath(): string {\n const dataDir = process.env.TA_DATA_DIR ?? 'data'\n return path.join(process.cwd(), dataDir, 'ta-admin.json')\n}\n\nexport function generateDefaultPassword(): string {\n return crypto.randomBytes(6).toString('hex') // 12-char hex, easy to type\n}\n\nexport function hashPassword(password: string): string {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt'\n return crypto.createHash('sha256').update(secret + password).digest('hex')\n}\n\nexport function loadAdmin(): AdminRecord | null {\n const filePath = adminFilePath()\n if (!fs.existsSync(filePath)) return null\n try {\n return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as AdminRecord\n } catch {\n return null\n }\n}\n\nexport function saveAdmin(record: AdminRecord): void {\n const filePath = adminFilePath()\n const dir = path.dirname(filePath)\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })\n fs.writeFileSync(filePath, JSON.stringify(record, null, 2), 'utf-8')\n}\n\nexport const DEFAULT_PASSWORD = 'Chang3M3Now!'\n\nexport function initAdmin(): { password: string; apiKey: string; isNew: boolean } {\n const existing = loadAdmin()\n if (existing) return { password: '', apiKey: '', isNew: false }\n\n const apiKey = generateApiKey()\n saveAdmin({\n passwordHash: hashPassword(DEFAULT_PASSWORD),\n isDefaultPassword: true,\n createdAt: new Date().toISOString(),\n lastLoginAt: null,\n apiKey: encryptApiKey(apiKey),\n })\n return { password: DEFAULT_PASSWORD, apiKey, isNew: true }\n}\n\nexport function verifyPassword(password: string): boolean {\n const record = loadAdmin()\n if (!record) return false\n return record.passwordHash === hashPassword(password)\n}\n\nexport function updatePassword(newPassword: string): void {\n const record = loadAdmin()\n if (!record) return\n saveAdmin({\n ...record,\n passwordHash: hashPassword(newPassword),\n isDefaultPassword: false,\n })\n}\n\nexport function recordLogin(): void {\n const record = loadAdmin()\n if (!record) return\n saveAdmin({ ...record, lastLoginAt: new Date().toISOString() })\n}\n\n// ---------------------------------------------------------------------------\n// API key — AES-256-GCM encrypted at rest, mirroring WP's SECURE_AUTH_KEY approach\n// ---------------------------------------------------------------------------\n\nconst CIPHER = 'aes-256-gcm'\n\nfunction getEncryptionKey(): Buffer {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-fallback-key-change-me'\n // Derive a 32-byte key from the secret using SHA-256\n return crypto.createHash('sha256').update(secret).digest()\n}\n\nfunction encryptApiKey(plaintext: string): string {\n const iv = crypto.randomBytes(12)\n const key = getEncryptionKey()\n const cipher = crypto.createCipheriv(CIPHER, key, iv) as crypto.CipherGCM\n const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()])\n const tag = cipher.getAuthTag()\n // Format: iv(24 hex) + tag(32 hex) + encrypted(hex)\n return iv.toString('hex') + tag.toString('hex') + encrypted.toString('hex')\n}\n\nfunction decryptApiKey(encoded: string): string | null {\n try {\n const iv = Buffer.from(encoded.slice(0, 24), 'hex')\n const tag = Buffer.from(encoded.slice(24, 56), 'hex')\n const encrypted = Buffer.from(encoded.slice(56), 'hex')\n const key = getEncryptionKey()\n const decipher = crypto.createDecipheriv(CIPHER, key, iv) as crypto.DecipherGCM\n decipher.setAuthTag(tag)\n return decipher.update(encrypted) + decipher.final('utf8')\n } catch {\n return null\n }\n}\n\nexport function generateApiKey(): string {\n return 'ta_' + crypto.randomBytes(24).toString('hex') // 51-char key\n}\n\nexport function getApiKey(): string | null {\n const record = loadAdmin()\n if (!record?.apiKey) return null\n return decryptApiKey(record.apiKey)\n}\n\nexport function rotateApiKey(): string {\n const record = loadAdmin()\n if (!record) throw new Error('Admin store not initialised')\n const newKey = generateApiKey()\n saveAdmin({ ...record, apiKey: encryptApiKey(newKey) })\n return newKey\n}\n\nexport function verifyApiKey(key: string): boolean {\n const stored = getApiKey()\n if (!stored) return false\n if (key.length !== stored.length) return false\n return crypto.timingSafeEqual(Buffer.from(key), Buffer.from(stored))\n}\n\n// ---------------------------------------------------------------------------\n// Session cookie: HMAC-SHA256(secret, userId + timestamp) — stateless, no DB\n// ---------------------------------------------------------------------------\nexport function signSession(payload: string): string {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt'\n const sig = crypto.createHmac('sha256', secret).update(payload).digest('hex')\n return `${payload}.${sig}`\n}\n\nexport function verifySession(token: string): boolean {\n const lastDot = token.lastIndexOf('.')\n if (lastDot === -1) return false\n const payload = token.slice(0, lastDot)\n const sig = token.slice(lastDot + 1)\n const expected = crypto.createHmac('sha256', process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt')\n .update(payload).digest('hex')\n // Constant-time comparison\n if (sig.length !== expected.length) return false\n return crypto.timingSafeEqual(Buffer.from(sig, 'hex'), Buffer.from(expected, 'hex'))\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAA+C;AAC/C,IAAAC,eAAiB;;;ACDjB,gBAAe;AACf,kBAAiB;AACjB,yBAAmB;AAaZ,IAAM,YAAN,MAAgB;AAAA,EAGrB,YAAY,SAA2B;AACrC,SAAK,aAAa,QAAQ;AAAA,EAC5B;AAAA;AAAA,EAGA,KAAK,MAA8B;AACjC,UAAM,aAAa;AAAA,MACjB,YAAAC,QAAK,KAAK,KAAK,YAAY,GAAG,IAAI,MAAM;AAAA,MACxC,YAAAA,QAAK,KAAK,KAAK,YAAY,GAAG,IAAI,KAAK;AAAA,MACvC,YAAAA,QAAK,KAAK,KAAK,YAAY,MAAM,WAAW;AAAA,MAC5C,YAAAA,QAAK,KAAK,KAAK,YAAY,MAAM,UAAU;AAAA,IAC7C;AAEA,eAAW,YAAY,YAAY;AACjC,UAAI,UAAAC,QAAG,WAAW,QAAQ,GAAG;AAC3B,eAAO,KAAK,UAAU,MAAM,QAAQ;AAAA,MACtC;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,UAAqB;AACnB,QAAI,CAAC,UAAAA,QAAG,WAAW,KAAK,UAAU,EAAG,QAAO,CAAC;AAC7C,WAAO,KAAK,QAAQ,KAAK,YAAY,KAAK,UAAU;AAAA,EACtD;AAAA,EAEQ,QAAQ,KAAa,MAAyB;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G,IAAI,QAAQ,QAAQ,KAAK,IAAI,QAAQ,IAAI;AAEjD,QAAM,QAAQ,OAAO,QAAQ;AAC7B,QAAM,QAAQ,cAAc,OAAO,OAAO;AAE1C,SAAO,4BAAa,KAAK;AAAA,IACvB;AAAA,IACA,OAAO;AAAA,MACL,OAAO,MAAM;AAAA,MACb,OAAO,MAAM,MAAM;AAAA,MACnB,OAAO,MAAM,MAAM;AAAA,IACrB;AAAA,IACA,UAAU,GAAG,OAAO;AAAA,EACtB,CAAC;AACH;","names":["import_server","import_path","path","fs","matter","import_fs","import_path","path","fs","crypto","crypto","crypto","crypto","path"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/dashboard/routes/okf-graph-route.ts","../../../src/core/mdx-reader.ts","../../../src/core/markdown-renderer.ts","../../../src/okf/okf-bundle.ts","../../../src/dashboard/auth.ts","../../../src/dashboard/admin-store.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server'\nimport path from 'path'\nimport { MdxReader } from '../../core/mdx-reader.js'\nimport { buildOkfGraph } from '../../okf/okf-bundle.js'\nimport { checkApiAuth, unauthorizedResponse } from '../auth.js'\n\nconst reader = new MdxReader({ contentDir: path.join(process.cwd(), process.env.TA_CONTENT_DIR ?? 'content') })\n\n/** GET /api/third-audience/okf-graph — returns graph JSON for the OKF dashboard viewer */\nexport async function GET(req: NextRequest): Promise<NextResponse> {\n if (!checkApiAuth(req)) return unauthorizedResponse()\n\n const baseUrl = process.env.NEXT_PUBLIC_SITE_URL\n ?? `${req.nextUrl.protocol}//${req.nextUrl.host}`\n\n const files = reader.readAll()\n const graph = buildOkfGraph(files, baseUrl)\n\n return NextResponse.json({\n graph,\n stats: {\n pages: files.length,\n nodes: graph.nodes.length,\n edges: graph.edges.length,\n },\n indexUrl: `${baseUrl}/okf`,\n })\n}\n","import fs from 'fs'\nimport path from 'path'\nimport matter from 'gray-matter'\n\nexport interface MdxFile {\n slug: string // relative path without extension, e.g. 'blog/my-post'\n filePath: string // absolute path to .mdx file\n frontmatter: Record<string, unknown>\n rawContent: string // body after frontmatter\n}\n\nexport interface MdxReaderOptions {\n contentDir: string // absolute path to content directory\n /** URL path segments to drop when mapping a request slug to a file. */\n stripSegments?: string[]\n}\n\nexport class MdxReader {\n private contentDir: string\n private stripSegments: string[]\n\n constructor(options: MdxReaderOptions) {\n this.contentDir = options.contentDir\n this.stripSegments = options.stripSegments ?? []\n }\n\n /**\n * Remove configured URL-only segments from a slug so it maps to the file\n * layout. e.g. stripSegments ['learn'] turns 'en/learn/hydroponics/x' into\n * 'en/hydroponics/x'. Only whole path segments are removed.\n */\n private applyStrip(slug: string): string {\n if (this.stripSegments.length === 0) return slug\n const drop = new Set(this.stripSegments)\n return slug\n .split('/')\n .filter((seg) => !drop.has(seg))\n .join('/')\n }\n\n /** Read a single MDX file by slug. Returns null if not found. */\n read(slug: string): MdxFile | null {\n const resolved = this.applyStrip(slug)\n const candidates = [\n path.join(this.contentDir, `${resolved}.mdx`),\n path.join(this.contentDir, `${resolved}.md`),\n path.join(this.contentDir, resolved, 'index.mdx'),\n path.join(this.contentDir, resolved, 'index.md'),\n ]\n\n for (const filePath of candidates) {\n if (fs.existsSync(filePath)) {\n return this.parseFile(slug, filePath)\n }\n }\n\n return null\n }\n\n /** Read all MDX files recursively. */\n readAll(): MdxFile[] {\n if (!fs.existsSync(this.contentDir)) return []\n return this.walkDir(this.contentDir, this.contentDir)\n }\n\n private walkDir(dir: string, root: string): MdxFile[] {\n const results: MdxFile[] = []\n for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {\n const fullPath = path.join(dir, entry.name)\n if (entry.isDirectory()) {\n results.push(...this.walkDir(fullPath, root))\n } else if (entry.name.endsWith('.mdx') || entry.name.endsWith('.md')) {\n const relative = path.relative(root, fullPath)\n const slug = relative.replace(/\\.(mdx|md)$/, '').replace(/\\/index$/, '')\n results.push(this.parseFile(slug, fullPath))\n }\n }\n return results\n }\n\n private parseFile(slug: string, filePath: string): MdxFile {\n const raw = fs.readFileSync(filePath, 'utf-8')\n const { data: frontmatter, content: rawContent } = matter(raw)\n return { slug, filePath, frontmatter, rawContent }\n }\n}\n","import type { MdxFile } from './mdx-reader.js'\n\n/**\n * Strips JSX from MDX content and returns clean Markdown\n * suitable for AI crawlers.\n *\n * Removes:\n * - import/export statements\n * - JSX component tags (<ComponentName ... /> and <ComponentName>...</ComponentName>)\n * - Inline expressions {variable} that aren't standard Markdown\n *\n * Preserves:\n * - All standard Markdown (headings, lists, code blocks, links, images)\n * - Frontmatter (serialized as YAML header)\n */\nexport class MarkdownRenderer {\n render(file: MdxFile): string {\n const header = this.buildFrontmatterHeader(file.frontmatter)\n const body = this.stripJsx(file.rawContent)\n return header ? `${header}\\n\\n${body}` : body\n }\n\n private buildFrontmatterHeader(fm: Record<string, unknown>): string {\n const keys = Object.keys(fm)\n if (keys.length === 0) return ''\n const lines = keys\n .filter(k => fm[k] !== undefined && fm[k] !== null)\n .map(k => `${k}: ${this.yamlValue(fm[k])}`)\n return `---\\n${lines.join('\\n')}\\n---`\n }\n\n private yamlValue(val: unknown): string {\n if (typeof val === 'string') {\n // Quote strings containing special YAML chars\n return /[:#\\[\\]{},&*?|<>=!%@`]/.test(val) ? `\"${val.replace(/\"/g, '\\\\\"')}\"` : val\n }\n if (val instanceof Date) return val.toISOString()\n if (Array.isArray(val)) return `[${val.map(v => this.yamlValue(v)).join(', ')}]`\n return String(val)\n }\n\n private stripJsx(content: string): string {\n let out = content\n\n // Remove import statements: import Foo from '...' / import { Foo } from '...'\n out = out.replace(/^import\\s+.*?['\"].*?['\"]\\s*\\n?/gm, '')\n\n // Remove export statements at line start (export const, export default, export { })\n out = out.replace(/^export\\s+(?:default\\s+)?(?:const|let|var|function|class)\\s+[\\s\\S]*?(?=\\n(?=[^{]|\\n)|\\n{2,})/gm, '')\n out = out.replace(/^export\\s*\\{[^}]*\\}\\s*(?:from\\s+['\"][^'\"]*['\"])?\\s*\\n?/gm, '')\n\n // Remove self-closing JSX tags: <Component ... />\n // Must not match HTML img/br/hr which are valid Markdown\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*\\/>/g, '')\n\n // Remove JSX block tags: <Component ...>...</Component>\n // Greedy but bounded by matching closing tag\n out = out.replace(/<([A-Z][A-Za-z0-9.]*)[^>]*>[\\s\\S]*?<\\/\\1>/g, '')\n\n // Remove JSX expression blocks { expression } that span a whole line\n out = out.replace(/^\\s*\\{[^}]+\\}\\s*\\n/gm, '')\n\n // Collapse multiple blank lines to two\n out = out.replace(/\\n{3,}/g, '\\n\\n')\n\n return out.trim()\n }\n}\n","import type { MdxFile } from '../core/mdx-reader.js'\nimport { MarkdownRenderer } from '../core/markdown-renderer.js'\n\nconst renderer = new MarkdownRenderer()\n\nexport interface OkfGraphNode {\n id: string\n title: string\n type: string\n url: string\n}\n\nexport interface OkfGraphEdge {\n source: string\n target: string\n}\n\nexport interface OkfGraphData {\n nodes: OkfGraphNode[]\n edges: OkfGraphEdge[]\n}\n\n/**\n * Builds the knowledge graph data for the OKF viewer.\n * Nodes = content pages; edges = internal links between them.\n * Trims to top 100 most-connected nodes (matching WP plugin behaviour).\n */\nexport function buildOkfGraph(files: MdxFile[], baseUrl: string): OkfGraphData {\n const base = baseUrl.replace(/\\/$/, '')\n const slugSet = new Set(files.map(f => f.slug))\n\n // Build slug → markdown map for link extraction\n const mdMap = new Map<string, string>()\n for (const file of files) {\n mdMap.set(file.slug, renderer.render(file))\n }\n\n // Count degrees to pick top 100\n const degrees = new Map<string, number>(files.map(f => [f.slug, 0]))\n const rawEdges: OkfGraphEdge[] = []\n\n for (const file of files) {\n const md = mdMap.get(file.slug) ?? ''\n const linkRe = /\\[([^\\]]+)\\]\\((\\/[^)]+)\\)/g\n let m: RegExpExecArray | null\n while ((m = linkRe.exec(md)) !== null) {\n const candidate = m[2].replace(/^\\//, '').replace(/\\.md$/, '')\n if (slugSet.has(candidate) && candidate !== file.slug) {\n rawEdges.push({ source: file.slug, target: candidate })\n degrees.set(file.slug, (degrees.get(file.slug) ?? 0) + 1)\n degrees.set(candidate, (degrees.get(candidate) ?? 0) + 1)\n }\n }\n }\n\n // Top 100 nodes by degree\n const top100 = files\n .slice()\n .sort((a, b) => (degrees.get(b.slug) ?? 0) - (degrees.get(a.slug) ?? 0))\n .slice(0, 100)\n const topSet = new Set(top100.map(f => f.slug))\n\n const nodes: OkfGraphNode[] = top100.map(f => ({\n id: f.slug,\n title: String(f.frontmatter.title ?? f.slug),\n type: String(f.frontmatter.type ?? 'WebPage'),\n url: `${base}/${f.slug}`,\n }))\n\n const edges = rawEdges.filter(e => topSet.has(e.source) && topSet.has(e.target))\n\n return { nodes, edges }\n}\n\n/** Generates the /okf/index.md manifest listing all content. */\nexport function generateOkfIndex(files: MdxFile[], baseUrl: string): string {\n const base = baseUrl.replace(/\\/$/, '')\n const lines = [\n '# Open Knowledge Format (OKF) Bundle',\n '',\n 'This bundle contains all content as clean Markdown files for AI consumption.',\n '',\n '## Contents',\n '',\n ]\n\n for (const file of files) {\n const fm = file.frontmatter\n const title = String(fm.title ?? file.slug)\n const desc = fm.description ? ` — ${String(fm.description)}` : ''\n lines.push(`- [${title}](${base}/okf/${file.slug}.md)${desc}`)\n }\n\n return lines.join('\\n') + '\\n'\n}\n\n/** Renders a single MDX file for OKF, with internal links rewritten to .md siblings. */\nexport function generateOkfPage(file: MdxFile, allFiles: MdxFile[], baseUrl: string): string {\n const markdown = renderer.render(file)\n return rewriteInternalLinks(markdown, allFiles, baseUrl)\n}\n\n/**\n * Rewrites internal links to point at sibling .md files in the OKF bundle.\n * e.g. [link](/blog/post) → [link](/okf/blog/post.md)\n */\nfunction rewriteInternalLinks(markdown: string, allFiles: MdxFile[], baseUrl: string): string {\n const slugSet = new Set(allFiles.map(f => f.slug))\n const base = baseUrl.replace(/\\/$/, '')\n\n return markdown.replace(/\\[([^\\]]+)\\]\\((\\/[^)]+)\\)/g, (match, text, href) => {\n // Strip leading slash and any trailing .md to get candidate slug\n const candidate = href.replace(/^\\//, '').replace(/\\.md$/, '')\n if (slugSet.has(candidate)) {\n return `[${text}](${base}/okf/${candidate}.md)`\n }\n return match\n })\n}\n","import type { NextRequest } from 'next/server'\nimport { NextResponse } from 'next/server'\nimport { verifySession, verifyApiKey } from './admin-store.js'\n\nconst SESSION_COOKIE = 'ta_session'\n\n/**\n * Authenticate an API route request. Accepts (in order):\n * 1. X-TA-Api-Key header — for headless/external callers (mirrors WP's approach)\n * 2. Authorization: Bearer <api-key> — same key, different transport\n * 3. Valid ta_session cookie — browser dashboard session\n */\nexport function checkApiAuth(req: NextRequest): boolean {\n // 1. X-TA-Api-Key header (WP-style headless key)\n const apiKeyHeader = req.headers.get('x-ta-api-key')\n if (apiKeyHeader) return verifyApiKey(apiKeyHeader)\n\n // 2. Bearer token (treat as api key)\n const auth = req.headers.get('authorization') ?? ''\n if (auth.startsWith('Bearer ')) {\n const token = auth.slice(7)\n return verifyApiKey(token)\n }\n\n // 3. Browser session cookie\n const session = req.cookies.get(SESSION_COOKIE)?.value\n if (session) return verifySession(session)\n\n return false\n}\n\n/**\n * Returns a 401 JSON response with the correct WWW-Authenticate header.\n * Use as: if (!checkApiAuth(req)) return unauthorizedResponse()\n */\nexport function unauthorizedResponse(): NextResponse {\n return NextResponse.json(\n { error: 'Unauthorized. Provide X-TA-Api-Key header or a valid session cookie.' },\n {\n status: 401,\n headers: { 'WWW-Authenticate': 'Bearer realm=\"Third Audience API\"' },\n }\n )\n}\n","import fs from 'fs'\nimport path from 'path'\nimport crypto from 'crypto'\n\nexport interface AdminRecord {\n passwordHash: string // sha256(secret + password)\n isDefaultPassword: boolean\n createdAt: string\n lastLoginAt: string | null\n apiKey?: string // AES-256-GCM encrypted, for headless/external API callers\n}\n\nfunction adminFilePath(): string {\n const dataDir = process.env.TA_DATA_DIR ?? 'data'\n return path.join(process.cwd(), dataDir, 'ta-admin.json')\n}\n\nexport function generateDefaultPassword(): string {\n return crypto.randomBytes(6).toString('hex') // 12-char hex, easy to type\n}\n\nexport function hashPassword(password: string): string {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt'\n return crypto.createHash('sha256').update(secret + password).digest('hex')\n}\n\nexport function loadAdmin(): AdminRecord | null {\n const filePath = adminFilePath()\n if (!fs.existsSync(filePath)) return null\n try {\n return JSON.parse(fs.readFileSync(filePath, 'utf-8')) as AdminRecord\n } catch {\n return null\n }\n}\n\nexport function saveAdmin(record: AdminRecord): void {\n const filePath = adminFilePath()\n const dir = path.dirname(filePath)\n if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })\n fs.writeFileSync(filePath, JSON.stringify(record, null, 2), 'utf-8')\n}\n\nexport const DEFAULT_PASSWORD = 'Chang3M3Now!'\n\nexport function initAdmin(): { password: string; apiKey: string; isNew: boolean } {\n const existing = loadAdmin()\n if (existing) return { password: '', apiKey: '', isNew: false }\n\n const apiKey = generateApiKey()\n saveAdmin({\n passwordHash: hashPassword(DEFAULT_PASSWORD),\n isDefaultPassword: true,\n createdAt: new Date().toISOString(),\n lastLoginAt: null,\n apiKey: encryptApiKey(apiKey),\n })\n return { password: DEFAULT_PASSWORD, apiKey, isNew: true }\n}\n\nexport function verifyPassword(password: string): boolean {\n const record = loadAdmin()\n if (!record) return false\n return record.passwordHash === hashPassword(password)\n}\n\nexport function updatePassword(newPassword: string): void {\n const record = loadAdmin()\n if (!record) return\n saveAdmin({\n ...record,\n passwordHash: hashPassword(newPassword),\n isDefaultPassword: false,\n })\n}\n\nexport function recordLogin(): void {\n const record = loadAdmin()\n if (!record) return\n saveAdmin({ ...record, lastLoginAt: new Date().toISOString() })\n}\n\n// ---------------------------------------------------------------------------\n// API key — AES-256-GCM encrypted at rest, mirroring WP's SECURE_AUTH_KEY approach\n// ---------------------------------------------------------------------------\n\nconst CIPHER = 'aes-256-gcm'\n\nfunction getEncryptionKey(): Buffer {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-fallback-key-change-me'\n // Derive a 32-byte key from the secret using SHA-256\n return crypto.createHash('sha256').update(secret).digest()\n}\n\nfunction encryptApiKey(plaintext: string): string {\n const iv = crypto.randomBytes(12)\n const key = getEncryptionKey()\n const cipher = crypto.createCipheriv(CIPHER, key, iv) as crypto.CipherGCM\n const encrypted = Buffer.concat([cipher.update(plaintext, 'utf8'), cipher.final()])\n const tag = cipher.getAuthTag()\n // Format: iv(24 hex) + tag(32 hex) + encrypted(hex)\n return iv.toString('hex') + tag.toString('hex') + encrypted.toString('hex')\n}\n\nfunction decryptApiKey(encoded: string): string | null {\n try {\n const iv = Buffer.from(encoded.slice(0, 24), 'hex')\n const tag = Buffer.from(encoded.slice(24, 56), 'hex')\n const encrypted = Buffer.from(encoded.slice(56), 'hex')\n const key = getEncryptionKey()\n const decipher = crypto.createDecipheriv(CIPHER, key, iv) as crypto.DecipherGCM\n decipher.setAuthTag(tag)\n return decipher.update(encrypted) + decipher.final('utf8')\n } catch {\n return null\n }\n}\n\nexport function generateApiKey(): string {\n return 'ta_' + crypto.randomBytes(24).toString('hex') // 51-char key\n}\n\nexport function getApiKey(): string | null {\n const record = loadAdmin()\n if (!record?.apiKey) return null\n return decryptApiKey(record.apiKey)\n}\n\nexport function rotateApiKey(): string {\n const record = loadAdmin()\n if (!record) throw new Error('Admin store not initialised')\n const newKey = generateApiKey()\n saveAdmin({ ...record, apiKey: encryptApiKey(newKey) })\n return newKey\n}\n\nexport function verifyApiKey(key: string): boolean {\n const stored = getApiKey()\n if (!stored) return false\n if (key.length !== stored.length) return false\n return crypto.timingSafeEqual(Buffer.from(key), Buffer.from(stored))\n}\n\n// ---------------------------------------------------------------------------\n// Session cookie: HMAC-SHA256(secret, userId + timestamp) — stateless, no DB\n// ---------------------------------------------------------------------------\nexport function signSession(payload: string): string {\n const secret = process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt'\n const sig = crypto.createHmac('sha256', secret).update(payload).digest('hex')\n return `${payload}.${sig}`\n}\n\nexport function verifySession(token: string): boolean {\n const lastDot = token.lastIndexOf('.')\n if (lastDot === -1) return false\n const payload = token.slice(0, lastDot)\n const sig = token.slice(lastDot + 1)\n const expected = crypto.createHmac('sha256', process.env.THIRD_AUDIENCE_SECRET ?? 'ta-salt')\n .update(payload).digest('hex')\n // Constant-time comparison\n if (sig.length !== expected.length) return false\n return crypto.timingSafeEqual(Buffer.from(sig, 'hex'), Buffer.from(expected, 'hex'))\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,iBAA+C;AAC/C,IAAAC,eAAiB;;;ACDjB,gBAAe;AACf,kBAAiB;AACjB,yBAAmB;AAeZ,IAAM,YAAN,MAAgB;AAAA,EAIrB,YAAY,SAA2B;AACrC,SAAK,aAAa,QAAQ;AAC1B,SAAK,gBAAgB,QAAQ,iBAAiB,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,WAAW,MAAsB;AACvC,QAAI,KAAK,cAAc,WAAW,EAAG,QAAO;AAC5C,UAAM,OAAO,IAAI,IAAI,KAAK,aAAa;AACvC,WAAO,KACJ,MAAM,GAAG,EACT,OAAO,CAAC,QAAQ,CAAC,KAAK,IAAI,GAAG,CAAC,EAC9B,KAAK,GAAG;AAAA,EACb;AAAA;AAAA,EAGA,KAAK,MAA8B;AACjC,UAAM,WAAW,KAAK,WAAW,IAAI;AACrC,UAAM,aAAa;AAAA,MACjB,YAAAC,QAAK,KAAK,KAAK,YAAY,GAAG,QAAQ,MAAM;AAAA,MAC5C,YAAAA,QAAK,KAAK,KAAK,YAAY,GAAG,QAAQ,KAAK;AAAA,MAC3C,YAAAA,QAAK,KAAK,KAAK,YAAY,UAAU,WAAW;AAAA,MAChD,YAAAA,QAAK,KAAK,KAAK,YAAY,UAAU,UAAU;AAAA,IACjD;AAEA,eAAW,YAAY,YAAY;AACjC,UAAI,UAAAC,QAAG,WAAW,QAAQ,GAAG;AAC3B,eAAO,KAAK,UAAU,MAAM,QAAQ;AAAA,MACtC;AAAA,IACF;AAEA,WAAO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,aAAAC,QAAK,KAAK,QAAQ,IAAI,GAAG,QAAQ,IAAI,kBAAkB,SAAS,EAAE,CAAC;AAG9G,eAAsB,IAAI,KAAyC;AACjE,MAAI,CAAC,aAAa,GAAG,EAAG,QAAO,qBAAqB;AAEpD,QAAM,UAAU,QAAQ,IAAI,wBACvB,GAAG,IAAI,QAAQ,QAAQ,KAAK,IAAI,QAAQ,IAAI;AAEjD,QAAM,QAAQ,OAAO,QAAQ;AAC7B,QAAM,QAAQ,cAAc,OAAO,OAAO;AAE1C,SAAO,4BAAa,KAAK;AAAA,IACvB;AAAA,IACA,OAAO;AAAA,MACL,OAAO,MAAM;AAAA,MACb,OAAO,MAAM,MAAM;AAAA,MACnB,OAAO,MAAM,MAAM;AAAA,IACrB;AAAA,IACA,UAAU,GAAG,OAAO;AAAA,EACtB,CAAC;AACH;","names":["import_server","import_path","path","fs","matter","import_fs","import_path","path","fs","crypto","crypto","crypto","crypto","path"]}
|
|
@@ -9,14 +9,26 @@ import matter from "gray-matter";
|
|
|
9
9
|
var MdxReader = class {
|
|
10
10
|
constructor(options) {
|
|
11
11
|
this.contentDir = options.contentDir;
|
|
12
|
+
this.stripSegments = options.stripSegments ?? [];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Remove configured URL-only segments from a slug so it maps to the file
|
|
16
|
+
* layout. e.g. stripSegments ['learn'] turns 'en/learn/hydroponics/x' into
|
|
17
|
+
* 'en/hydroponics/x'. Only whole path segments are removed.
|
|
18
|
+
*/
|
|
19
|
+
applyStrip(slug) {
|
|
20
|
+
if (this.stripSegments.length === 0) return slug;
|
|
21
|
+
const drop = new Set(this.stripSegments);
|
|
22
|
+
return slug.split("/").filter((seg) => !drop.has(seg)).join("/");
|
|
12
23
|
}
|
|
13
24
|
/** Read a single MDX file by slug. Returns null if not found. */
|
|
14
25
|
read(slug) {
|
|
26
|
+
const resolved = this.applyStrip(slug);
|
|
15
27
|
const candidates = [
|
|
16
|
-
path.join(this.contentDir, `${
|
|
17
|
-
path.join(this.contentDir, `${
|
|
18
|
-
path.join(this.contentDir,
|
|
19
|
-
path.join(this.contentDir,
|
|
28
|
+
path.join(this.contentDir, `${resolved}.mdx`),
|
|
29
|
+
path.join(this.contentDir, `${resolved}.md`),
|
|
30
|
+
path.join(this.contentDir, resolved, "index.mdx"),
|
|
31
|
+
path.join(this.contentDir, resolved, "index.md")
|
|
20
32
|
];
|
|
21
33
|
for (const filePath of candidates) {
|
|
22
34
|
if (fs.existsSync(filePath)) {
|