generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownToAsciiDoc.ts
55 lines (47 loc) · 1.66 KB
/
markdownToAsciiDoc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export function markdownToAsciiDoc(markdown: string): string {
// Split the input markdown string into lines
const lines = markdown.split("\n").filter((line) => line.trim() !== "");
// Check if the second line is a separator line
const separatorLine = lines[1].trim();
// Regex explanation:
// ^: Asserts the start of the line.
// (\|\s*-+\s*)+: Matches one or more occurrences of:
// \|: A literal pipe character.
// \s*: Zero or more whitespace characters.
// [-:]+: One or more `-` or `:`
// \s*: Zero or more whitespace characters.
// \|: A literal pipe character at the end.
// $: Asserts the end of the line.
if (!/^(\|\s*[-:]+\s*)+\|$/.test(separatorLine)) {
throw new Error("Invalid markdown table: missing separator line.");
}
// Extract the header and rows
const header = lines[0]
.split("|")
.map((cell) => convertLinks(cell.trim()))
.slice(1, -1);
const rows = lines.slice(2).map((line) =>
line
.split("|")
.map((cell) => convertLinks(cell.trim()))
.slice(1, -1),
);
// Build the AsciiDoc table
let asciidoc = `\`\`\`asciidoc-table\n[cols="${"1,"
.repeat(header.length)
.slice(0, -1)}", options="header"]\n|===\n`;
// Add the header row
asciidoc += header.map((cell) => `| ${cell}`).join("\n") + "\n\n";
// Add the data rows
rows.forEach((row) => {
asciidoc += row.map((cell) => `| ${cell}`).join("\n") + "\n\n";
});
// Close the AsciiDoc table
asciidoc += "|===\n```\n";
return asciidoc;
}
// Helper function to convert Markdown links to AsciiDoc links
function convertLinks(text: string): string {
const markdownLinkRegex = /\[([^\]]+)\]\(([^)]+)\)/g;
return text.replace(markdownLinkRegex, "link:$2[$1]");
}