feat(baoyu-danger-x-to-markdown): add MARKDOWN entity support for X articles
This commit is contained in:
parent
7cc9c92722
commit
0b9e51d6cc
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { expect, test } from "bun:test";
|
||||||
|
|
||||||
|
import { formatArticleMarkdown } from "./markdown.js";
|
||||||
|
|
||||||
|
test("formatArticleMarkdown renders MARKDOWN entities from atomic blocks", () => {
|
||||||
|
const article = {
|
||||||
|
title: "Atomic Markdown Example",
|
||||||
|
content_state: {
|
||||||
|
blocks: [
|
||||||
|
{
|
||||||
|
type: "unstyled",
|
||||||
|
text: "Before the snippet.",
|
||||||
|
entityRanges: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "atomic",
|
||||||
|
text: " ",
|
||||||
|
entityRanges: [{ key: 0, offset: 0, length: 1 }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "unstyled",
|
||||||
|
text: "After the snippet.",
|
||||||
|
entityRanges: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
entityMap: {
|
||||||
|
"0": {
|
||||||
|
key: "5",
|
||||||
|
value: {
|
||||||
|
type: "MARKDOWN",
|
||||||
|
mutability: "Mutable",
|
||||||
|
data: {
|
||||||
|
markdown: "```python\nprint('hello from x article')\n```\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const { markdown } = formatArticleMarkdown(article);
|
||||||
|
|
||||||
|
expect(markdown).toContain("Before the snippet.");
|
||||||
|
expect(markdown).toContain("```python\nprint('hello from x article')\n```");
|
||||||
|
expect(markdown).toContain("After the snippet.");
|
||||||
|
expect(markdown).toBe(`# Atomic Markdown Example
|
||||||
|
|
||||||
|
Before the snippet.
|
||||||
|
|
||||||
|
\`\`\`python
|
||||||
|
print('hello from x article')
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
After the snippet.`);
|
||||||
|
});
|
||||||
|
|
@ -237,6 +237,22 @@ function resolveEntityTweetLines(
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveEntityMarkdownLines(
|
||||||
|
entityKey: number | undefined,
|
||||||
|
entityMap: ArticleContentState["entityMap"] | undefined,
|
||||||
|
entityLookup: EntityLookup
|
||||||
|
): string[] {
|
||||||
|
if (entityKey === undefined) return [];
|
||||||
|
const entry = resolveEntityEntry(entityKey, entityMap, entityLookup);
|
||||||
|
const value = entry?.value;
|
||||||
|
if (!value || value.type !== "MARKDOWN") return [];
|
||||||
|
|
||||||
|
const markdown = typeof value.data?.markdown === "string" ? value.data.markdown : "";
|
||||||
|
const normalized = markdown.replace(/\r\n/g, "\n").trimEnd();
|
||||||
|
if (!normalized) return [];
|
||||||
|
return normalized.split("\n");
|
||||||
|
}
|
||||||
|
|
||||||
function buildMediaLinkMap(
|
function buildMediaLinkMap(
|
||||||
entityMap: ArticleContentState["entityMap"] | undefined
|
entityMap: ArticleContentState["entityMap"] | undefined
|
||||||
): Map<number, string> {
|
): Map<number, string> {
|
||||||
|
|
@ -397,6 +413,16 @@ function renderContentBlocks(
|
||||||
return [...new Set(linkLines)];
|
return [...new Set(linkLines)];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const collectMarkdownLines = (block: ArticleBlock): string[] => {
|
||||||
|
const ranges = Array.isArray(block.entityRanges) ? block.entityRanges : [];
|
||||||
|
const markdownLines: string[] = [];
|
||||||
|
for (const range of ranges) {
|
||||||
|
if (typeof range?.key !== "number") continue;
|
||||||
|
markdownLines.push(...resolveEntityMarkdownLines(range.key, entityMap, entityLookup));
|
||||||
|
}
|
||||||
|
return markdownLines;
|
||||||
|
};
|
||||||
|
|
||||||
const pushTrailingMedia = (mediaLines: string[]) => {
|
const pushTrailingMedia = (mediaLines: string[]) => {
|
||||||
if (mediaLines.length > 0) {
|
if (mediaLines.length > 0) {
|
||||||
pushBlock(mediaLines, "media");
|
pushBlock(mediaLines, "media");
|
||||||
|
|
@ -441,6 +467,11 @@ function renderContentBlocks(
|
||||||
pushBlock(tweetLines, "quote");
|
pushBlock(tweetLines, "quote");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const markdownLines = collectMarkdownLines(block);
|
||||||
|
if (markdownLines.length > 0) {
|
||||||
|
pushBlock(markdownLines, "text");
|
||||||
|
}
|
||||||
|
|
||||||
const mediaLines = collectMediaLines(block);
|
const mediaLines = collectMediaLines(block);
|
||||||
if (mediaLines.length > 0) {
|
if (mediaLines.length > 0) {
|
||||||
pushBlock(mediaLines, "media");
|
pushBlock(mediaLines, "media");
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,7 @@ export type ArticleEntityMapEntry = {
|
||||||
mutability?: string;
|
mutability?: string;
|
||||||
data?: {
|
data?: {
|
||||||
caption?: string;
|
caption?: string;
|
||||||
|
markdown?: string;
|
||||||
mediaItems?: ArticleEntityMapMediaItem[];
|
mediaItems?: ArticleEntityMapMediaItem[];
|
||||||
url?: string;
|
url?: string;
|
||||||
tweetId?: string;
|
tweetId?: string;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue