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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/core/src/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export interface PreprocessConfig {
/** 将表格转换为文本(用 | 分隔列,适用于不支持表格的平台) */
convertTablesToText?: boolean

/** 清理源平台链接(去除站内链接、还原跳转中转) */
cleanPlatformLinks?: boolean
/** 需要去除的站内链接域名(仅保留文本) */
removeLinkDomains?: string[]
/** 需要还原的跳转中转域名(将 href 替换为 target 参数中的实际 URL) */
redirectLinkDomains?: string[]

/** 保留 <style> 标签(CLI 同步自定义 HTML 时使用) */
keepStyles?: boolean

Expand Down Expand Up @@ -96,6 +103,9 @@ export const DEFAULT_PREPROCESS_CONFIG: PreprocessConfig = {
removeDataAttributes: true,
removeSrcset: true,
removeSizes: true,
cleanPlatformLinks: true,
removeLinkDomains: ['zhida.zhihu.com'],
redirectLinkDomains: ['link.zhihu.com'],
}

/**
Expand Down
35 changes: 35 additions & 0 deletions packages/extension/src/lib/content-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export function preprocessForPlatform(rawHtml: string, config: PreprocessConfig)
// 移除 script 和 noscript(总是执行),style 根据配置决定
removeElements(container, config.keepStyles ? ['script', 'noscript'] : ['script', 'style', 'noscript'])

if (config.cleanPlatformLinks) {
cleanPlatformLinks(container, config.removeLinkDomains, config.redirectLinkDomains)
}

if (config.removeLinks) {
processLinks(container, config.keepLinkDomains)
}
Expand Down Expand Up @@ -235,6 +239,37 @@ function processSvgImages(container: HTMLElement): void {
})
}

/**
* 清理源平台链接:去除站内链接(保留文本)、还原跳转中转(替换为实际 URL)
*/
function cleanPlatformLinks(container: HTMLElement, removeDomains?: string[], redirectDomains?: string[]): void {
const links = Array.from(container.querySelectorAll('a'))

for (const link of links) {
const href = link.getAttribute('href') || ''

if (removeDomains?.some(domain => href.includes(domain))) {
const parent = link.parentNode
if (!parent) continue
while (link.firstChild) {
parent.insertBefore(link.firstChild, link)
}
parent.removeChild(link)
continue
}

if (redirectDomains?.some(domain => href.includes(domain))) {
try {
const url = new URL(href)
const target = url.searchParams.get('target')
if (target) {
link.setAttribute('href', target)
}
} catch {}
}
}
}

/**
* 处理链接
*/
Expand Down