-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
73 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,16 @@ | ||
import { marked } from "marked"; | ||
import { BaseParser, type ParserOptions } from "../core/base-parser.js"; | ||
import { BaseParser } from "../core/base-parser.ts"; | ||
import type { ParserOptions } from "../core/types.ts"; | ||
|
||
export class MarkdownParser extends BaseParser { | ||
async parse(input: string): Promise<Record<string, string>> { | ||
try { | ||
const tokens = marked.lexer(input); | ||
const result: Record<string, string> = {}; | ||
|
||
// Extract text content from markdown tokens | ||
for (const token of tokens) { | ||
if (token.type === "heading") { | ||
const key = `heading.${token.depth}`; | ||
result[key] = token.text; | ||
} else if (token.type === "paragraph") { | ||
result[`paragraph.${Object.keys(result).length}`] = token.text; | ||
} else if (token.type === "list") { | ||
token.items.forEach((item: { text: string }, index: number) => { | ||
result[`list.${index}`] = item.text; | ||
}); | ||
} | ||
} | ||
|
||
return result; | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to parse Markdown translations: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
return {}; | ||
} | ||
|
||
async serialize( | ||
_locale: string, | ||
data: Record<string, string>, | ||
_originalData?: Record<string, string>, | ||
originalData?: string | Record<string, unknown>, | ||
): Promise<string> { | ||
try { | ||
// Validate input data | ||
for (const [key, value] of Object.entries(data)) { | ||
if (value === undefined) { | ||
throw new Error(`Value for key "${key}" is undefined`); | ||
} | ||
} | ||
|
||
const markdownParts: string[] = []; | ||
|
||
for (const [key, value] of Object.entries(data)) { | ||
if (key.startsWith("heading.")) { | ||
const depth = Number.parseInt(key.split(".")[1], 10); | ||
markdownParts.push(`${"#".repeat(depth)} ${value}\n`); | ||
} else if (key.startsWith("paragraph.")) { | ||
markdownParts.push(`${value}\n\n`); | ||
} else if (key.startsWith("list.")) { | ||
markdownParts.push(`- ${value}\n`); | ||
} | ||
} | ||
|
||
return markdownParts.join(""); | ||
} catch (error) { | ||
throw new Error( | ||
`Failed to serialize Markdown translations: ${error instanceof Error ? error.message : String(error)}`, | ||
); | ||
} | ||
return ""; | ||
} | ||
} | ||
|
||
export function createMarkdownParser(options: ParserOptions): MarkdownParser { | ||
return new MarkdownParser(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.