datocms-html-to-structured-text
Version:
Convert HTML (or a `hast` syntax tree) to a valid DatoCMS Structured Text `dast` document
28 lines (24 loc) • 732 B
text/typescript
import { visit, SKIP } from 'unist-util-visit';
import type { Root as HastRoot, Element as HastElement } from 'hast';
export default function preprocessGoogleDocs(tree: HastRoot): void {
visit(tree, 'element', (node, index, parent) => {
if (
!isGoogleDocsWrapper(node) ||
!parent ||
index === undefined ||
!('children' in parent)
) {
return;
}
parent.children.splice(index, 1, ...node.children);
return [SKIP, index];
});
}
function isGoogleDocsWrapper(node: HastElement): boolean {
return (
node.tagName === 'b' &&
typeof node.properties === 'object' &&
typeof node.properties.id === 'string' &&
node.properties.id.startsWith('docs-internal-guid-')
);
}