Add Markdown to Post Format command and fix non-breaking space before period

- New markdownToLinkedIn() function converts Markdown to LinkedIn/Facebook-ready text:
  ## headings → Unicode bold, **bold** → Unicode bold, _italic_ → Unicode italic,
  - list items → em-dash bullets; blank lines and heading spacing handled correctly
- New "Convert Markdown to Post Format" command registered in commands.ts
- Fix space-before-period cleanup to also strip U+00A0 (non-breaking space),
  which Obsidian inserts after bold/formatted text (/ \./g → /[ \u00A0]\./g)
- Update README to document the new command and updated function list

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-21 17:15:32 +01:00
parent 6eedd47805
commit 0ecf6835c9
3 changed files with 66 additions and 3 deletions
+11 -1
View File
@@ -1,5 +1,5 @@
import { Plugin } from "obsidian";
import { transformText, cleanText, bulletToEmdash, bulletToArrow, numberedListSlash, numberedListParens, FormatStyle } from "./formatter";
import { transformText, cleanText, bulletToEmdash, bulletToArrow, numberedListSlash, numberedListParens, markdownToLinkedIn, FormatStyle } from "./formatter";
function addFormatCommand(plugin: Plugin, style: FormatStyle, name: string) {
plugin.addCommand({
@@ -68,4 +68,14 @@ export function registerCommands(plugin: Plugin): void {
}
},
});
plugin.addCommand({
id: "unicode-formatter:markdown-to-linkedin",
name: "Convert Markdown to Post Format",
editorCallback: (editor) => {
const selection = editor.getSelection();
if (selection) {
editor.replaceSelection(markdownToLinkedIn(selection));
}
},
});
}
+51
View File
@@ -46,3 +46,54 @@ export function numberedListParens(text: string): string {
return `(${n}) ${content}`;
}).join("\n");
}
export function applyInlineMarkdown(text: string): string {
return text.replace(/\*\*(.+?)\*\*|_(.+?)_/g, (_match, bold, italic) => {
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("- ")) {
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();
}