Browse Source

add link processing

master
Silberengel 2 weeks ago
parent
commit
80b70a87d0
  1. 1
      src/parser.ts
  2. 23
      src/processors/asciidoc.ts

1
src/parser.ts

@ -72,6 +72,7 @@ export class Parser {
enableLaTeX: this.options.enableLaTeX, enableLaTeX: this.options.enableLaTeX,
enableMusicalNotation: this.options.enableMusicalNotation, enableMusicalNotation: this.options.enableMusicalNotation,
originalContent: content, // Pass original for LaTeX detection originalContent: content, // Pass original for LaTeX detection
linkBaseURL: this.options.linkBaseURL, // Pass linkBaseURL for link processing
} }
); );

23
src/processors/asciidoc.ts

@ -1,6 +1,6 @@
import asciidoctor from '@asciidoctor/core'; import asciidoctor from '@asciidoctor/core';
import { ProcessResult } from '../types'; import { ProcessResult } from '../types';
import { extractTOC, sanitizeHTML } from './html-utils'; import { extractTOC, sanitizeHTML, processLinks } from './html-utils';
import { postProcessHtml } from './html-postprocess'; import { postProcessHtml } from './html-postprocess';
const asciidoctorInstance = asciidoctor(); const asciidoctorInstance = asciidoctor();
@ -10,6 +10,7 @@ export interface ProcessOptions {
enableLaTeX?: boolean; enableLaTeX?: boolean;
enableMusicalNotation?: boolean; enableMusicalNotation?: boolean;
originalContent?: string; // Original content for LaTeX detection originalContent?: string; // Original content for LaTeX detection
linkBaseURL?: string; // Base URL for link processing
} }
/** /**
@ -93,12 +94,22 @@ export async function processAsciidoc(
enableMusicalNotation, enableMusicalNotation,
}); });
// Process links: add target="_blank" to external links
const processedWithLinks = options.linkBaseURL
? processLinks(processed, options.linkBaseURL)
: processed;
// Also process TOC // Also process TOC
const tocSanitized = sanitizeHTML(toc); const tocSanitized = sanitizeHTML(toc);
const tocProcessed = postProcessHtml(tocSanitized, { const tocProcessed = postProcessHtml(tocSanitized, {
enableMusicalNotation: false, // Don't process music in TOC enableMusicalNotation: false, // Don't process music in TOC
}); });
// Process links in TOC as well
const tocProcessedWithLinks = options.linkBaseURL
? processLinks(tocProcessed, options.linkBaseURL)
: tocProcessed;
// Check for LaTeX in original content (more reliable than checking HTML) // Check for LaTeX in original content (more reliable than checking HTML)
const contentToCheck = options.originalContent || content; const contentToCheck = options.originalContent || content;
const hasLaTeX = enableLaTeX && hasMathContent(contentToCheck); const hasLaTeX = enableLaTeX && hasMathContent(contentToCheck);
@ -109,8 +120,8 @@ export async function processAsciidoc(
); );
return { return {
content: processed, content: processedWithLinks,
tableOfContents: tocProcessed, tableOfContents: tocProcessedWithLinks,
hasLaTeX, hasLaTeX,
hasMusicalNotation, hasMusicalNotation,
nostrLinks: [], // Will be populated by metadata extraction nostrLinks: [], // Will be populated by metadata extraction
@ -123,8 +134,10 @@ export async function processAsciidoc(
// Fallback to plain text with error logging // Fallback to plain text with error logging
const errorMessage = error instanceof Error ? error.message : String(error); const errorMessage = error instanceof Error ? error.message : String(error);
// Use process.stderr.write for Node.js compatibility instead of console.error // Use process.stderr.write for Node.js compatibility instead of console.error
if (typeof process !== 'undefined' && process.stderr) { // eslint-disable-next-line @typescript-eslint/no-explicit-any
process.stderr.write(`Error processing AsciiDoc: ${errorMessage}\n`); const nodeProcess = (globalThis as any).process;
if (nodeProcess?.stderr) {
nodeProcess.stderr.write(`Error processing AsciiDoc: ${errorMessage}\n`);
} }
// Escape HTML in content for safe display // Escape HTML in content for safe display

Loading…
Cancel
Save