All files / apps/huella-legal/layers/articles/server/mappers readerComment.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 4/4
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21          3x 14x 25x 17x   14x     14x              
import type { ReaderComment } from "../../shared/types/ReaderComment";
import type { WordpressComment } from "../adapters/wordpress";
import { parseFragment, textContent } from "../utils/hast";
 
// Readers' HTML never reaches the page: each block WordPress wrapped the comment in becomes plain text
const toParagraphs = (html: string): string[] => {
    const root = parseFragment(html);
    const blocks = root.children.filter((node) => node.type !== "text" || node.value.trim());
    const paragraphs = blocks.every((node) => node.type === "element") ? blocks : [root];
 
    return paragraphs.map(textContent).filter(Boolean);
};
 
export const toReaderComment = (comment: WordpressComment, replyTo?: string): ReaderComment => ({
    id: String(comment.id),
    author: comment.author_name.trim(),
    publishedAt: `${comment.date_gmt}Z`,
    paragraphs: toParagraphs(comment.content.rendered),
    replyTo,
});