mirror of
https://github.com/JimLiu/baoyu-skills.git
synced 2026-07-12 05:51:44 +08:00
e5d6c8ec68
- New parsers/ module with pluggable rule system for site-specific HTML extraction - X status parser: extract tweet text, media, quotes, author from data-testid elements - X article parser: extract long-form article content with inline media - archive.ph parser: restore original URL and prefer #CONTENT container - Improved slug generation with stop words and content-aware slugs - Output path uses subdirectory structure (domain/slug/slug.md) - Fix: preserve anchor elements containing media in legacy converter - Fix: smarter title deduplication in markdown document builder
48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import {
|
|
isMarkdownUsable,
|
|
normalizeMarkdown,
|
|
parseDocument,
|
|
type ConversionResult,
|
|
type PageMetadata,
|
|
} from "../markdown-conversion-shared.js";
|
|
import { URL_RULE_PARSERS } from "./rules/index.js";
|
|
import type { UrlRuleParserContext } from "./types.js";
|
|
|
|
export type { UrlRuleParser, UrlRuleParserContext } from "./types.js";
|
|
|
|
export function tryUrlRuleParsers(
|
|
html: string,
|
|
url: string,
|
|
baseMetadata: PageMetadata
|
|
): ConversionResult | null {
|
|
const document = parseDocument(html);
|
|
const context: UrlRuleParserContext = {
|
|
html,
|
|
url,
|
|
document,
|
|
baseMetadata,
|
|
};
|
|
|
|
for (const parser of URL_RULE_PARSERS) {
|
|
if (!parser.supports(context)) continue;
|
|
|
|
try {
|
|
const result = parser.parse(context);
|
|
if (!result) continue;
|
|
|
|
const markdown = normalizeMarkdown(result.markdown);
|
|
if (!isMarkdownUsable(markdown, html)) continue;
|
|
|
|
return {
|
|
...result,
|
|
markdown,
|
|
};
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
console.warn(`[url-to-markdown] parser ${parser.id} failed: ${message}`);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|