Add enhanced Markdown to LinkedIn conversion and X thread splitter
This commit is contained in:
+1
-50
@@ -75,53 +75,4 @@ export function numberedListParens(text: string): string {
|
||||
}).join("\n");
|
||||
}
|
||||
|
||||
export function applyInlineMarkdown(text: string): string {
|
||||
return text.replace(/\*\*(.+?)\*\*|_(.+?)_/g, (_match, bold: string | undefined, italic: string | undefined) => {
|
||||
if (bold !== undefined) return transformText(bold, "bold");
|
||||
return transformText(italic!, "italic");
|
||||
});
|
||||
}
|
||||
|
||||
export function markdownToLinkedIn(text: string): string {
|
||||
const lines = text.split("\n");
|
||||
const output: string[] = [];
|
||||
let pendingBlanks = 0;
|
||||
let skipNextBlanks = false;
|
||||
let inListContext = false;
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim() === "") {
|
||||
if (!skipNextBlanks) pendingBlanks++;
|
||||
continue;
|
||||
}
|
||||
|
||||
skipNextBlanks = false;
|
||||
|
||||
if (line.startsWith("## ")) {
|
||||
for (let i = 0; i < pendingBlanks; i++) output.push("");
|
||||
pendingBlanks = 0;
|
||||
inListContext = false;
|
||||
const headingText = line.slice(3);
|
||||
output.push(transformText(applyInlineMarkdown(headingText), "bold") + " ");
|
||||
skipNextBlanks = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith("- ") || line.startsWith("* ")) {
|
||||
if (!inListContext) {
|
||||
for (let i = 0; i < pendingBlanks; i++) output.push("");
|
||||
}
|
||||
pendingBlanks = 0;
|
||||
output.push("— " + applyInlineMarkdown(line.slice(2)) + " ");
|
||||
inListContext = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let i = 0; i < pendingBlanks; i++) output.push("");
|
||||
pendingBlanks = 0;
|
||||
inListContext = false;
|
||||
output.push(applyInlineMarkdown(line));
|
||||
}
|
||||
|
||||
return output.join("\n").replace(/[ \u00A0]\./g, ".").trimEnd();
|
||||
}
|
||||
export { markdownToLinkedIn } from "./linkedin";
|
||||
|
||||
Reference in New Issue
Block a user