pages235/src/markdownToFormattedText.ts
gguio 85c0eb8c5b
feat: Sign editor with formatting support! (+2 settings) (#86)
Co-authored-by: gguio <nikvish150@gmail.com>
Co-authored-by: Vitaly Turovsky <vital2580@icloud.com>
2024-03-23 16:21:36 +03:00

58 lines
1.4 KiB
TypeScript

import { remark } from 'remark'
export default (markdown: string) => {
const arr = markdown.split('\n\n')
const lines = ['', '', '', '']
for (const [i, ast] of arr.map(md => remark().parse(md)).entries()) {
lines[i] = transformToMinecraftJSON(ast as Element)
}
return lines
}
function transformToMinecraftJSON (element: Element): any {
switch (element.type) {
case 'root': {
if (!element.children) return
return element.children.map(child => transformToMinecraftJSON(child)).filter(Boolean)
}
case 'paragraph': {
if (!element.children) return
const transformedChildren = element.children.map(child => transformToMinecraftJSON(child)).filter(Boolean)
return transformedChildren.flat()
}
case 'strong': {
if (!element.children) return
return [{ bold: true, text: element.children[0].value }]
}
case 'text': {
return { text: element.value }
}
case 'emphasis': {
if (!element.children) return
return [{ italic: true, text: element.children[0].value }]
}
default:
// todo leave untouched eg links
return element.value
}
}
interface Position {
start: {
line: number;
column: number;
offset: number;
};
end: {
line: number;
column: number;
offset: number;
};
}
interface Element {
type: string;
children?: Element[];
value?: string;
position: Position;
}