mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-13 16:16:46 +08:00
34 lines
818 B
TypeScript
34 lines
818 B
TypeScript
import type { RootContentMap } from 'hast';
|
|
|
|
type HastUnionType<
|
|
K extends keyof RootContentMap,
|
|
V extends RootContentMap[K],
|
|
> = V;
|
|
|
|
export function onlyContainImgElement(
|
|
ast: HastUnionType<keyof RootContentMap, RootContentMap[keyof RootContentMap]>
|
|
): 'yes' | 'no' | 'maybe' {
|
|
if (ast.type === 'element') {
|
|
switch (ast.tagName) {
|
|
case 'html':
|
|
case 'body':
|
|
return ast.children.map(onlyContainImgElement).reduce((a, b) => {
|
|
if (a === 'no' || b === 'no') {
|
|
return 'no';
|
|
}
|
|
if (a === 'maybe' && b === 'maybe') {
|
|
return 'maybe';
|
|
}
|
|
return 'yes';
|
|
}, 'maybe');
|
|
case 'img':
|
|
return 'yes';
|
|
case 'head':
|
|
return 'maybe';
|
|
default:
|
|
return 'no';
|
|
}
|
|
}
|
|
return 'maybe';
|
|
}
|