Skip to content

Commit b9bd80f

Browse files
committed
add markdown support
1 parent b199c80 commit b9bd80f

File tree

6 files changed

+90
-39
lines changed

6 files changed

+90
-39
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,5 @@ Sample input file(json format):
9494

9595
* `auto`: Automatically detect the file format.
9696
* `json`: JSON format.
97-
* `yaml`: YAML format.
97+
* `yaml`: YAML format.
98+
* `markdown`: Markdown format.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"devDependencies": {
4141
"@types/flat": "^5.0.2",
4242
"@types/js-yaml": "^4.0.5",
43+
"@types/marked": "^5.0.0",
4344
"@types/mocha": "latest",
4445
"@types/node": "latest",
4546
"@types/yargs": "^17.0.24",
@@ -57,6 +58,7 @@
5758
"flat": "^5.0.2",
5859
"js-yaml": "^4.1.0",
5960
"langchain": "^0.0.70",
61+
"marked": "^5.1.0",
6062
"ora": "^5.0.0",
6163
"yargs": "^17.7.2"
6264
}

pnpm-lock.yaml

Lines changed: 38 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/formatter/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { extname } from 'path';
22
import json from './json';
33
import yaml from './yaml';
4+
import markdown from './markdown';
45

56
export type FormatterType = {
67
type: 'json',
78
formatter: json
89
} | {
910
type: 'yaml',
1011
formatter: yaml
12+
} | {
13+
type: 'markdown',
14+
formatter: markdown
1115
};
1216

1317
export interface Formatter {
@@ -21,6 +25,8 @@ export function getFormatter<T extends FormatterType>(type: T['type']): T['forma
2125
return new json();
2226
case 'yaml':
2327
return new yaml();
28+
case 'markdown':
29+
return new markdown();
2430
default:
2531
throw new Error(`Unknown formatter type: ${type}`);
2632
}
@@ -35,6 +41,8 @@ export function detectFormatterType(fileName: string): FormatterType['type'] {
3541
case '.yaml':
3642
case '.yml':
3743
return 'yaml';
44+
case '.md':
45+
return 'markdown';
3846
default:
3947
throw new Error(`Unknown file extension: ${ext}`);
4048
}

src/formatter/markdown.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Formatter } from ".";
2+
import { marked } from 'marked';
3+
4+
const PREFIX = 'p_';
5+
6+
type Links = {
7+
[key: string]: { href: string | null; title: string | null };
8+
};
9+
10+
export default class implements Formatter {
11+
private links: Links = {};
12+
13+
unmarshal(text: string): any {
14+
const tokens = marked.lexer(text);
15+
const result: Record<string, string> = {};
16+
17+
if (tokens.links) {
18+
this.links = tokens.links;
19+
}
20+
21+
for (const [i, token] of tokens.entries()) {
22+
result[PREFIX + i] = token.raw;
23+
}
24+
25+
return result;
26+
}
27+
28+
marshal(data: any, indent: number): string {
29+
let links = '';
30+
31+
if (this.links) {
32+
for (const [key, link] of Object.entries(this.links)) {
33+
const suffix = link.title ? ` "${link.title}"` : '';
34+
links += `[${key}]: ${link.href}${suffix}\n`;
35+
}
36+
}
37+
38+
return Object.values(data).join('') + links;
39+
}
40+
}

src/index.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import yargs from 'yargs';
2-
import { hideBin } from 'yargs/helpers';
31
import { ChatOpenAI } from 'langchain/chat_models/openai';
42
import {
53
ChatPromptTemplate,
@@ -15,20 +13,6 @@ import ora from 'ora';
1513
import languageEncoding from 'detect-file-encoding-and-language';
1614
import detectIndent from 'detect-indent';
1715

18-
yargs(hideBin(process.argv))
19-
.command('tc [file]', 'Translate formated file', (yargs) => {
20-
return yargs.positional('file', {
21-
describe: 'File to translate',
22-
type: 'string',
23-
default: 'lang.json',
24-
});
25-
})
26-
.option('key', {
27-
alias: 'k',
28-
type: 'string',
29-
description: 'OpenAI API key',
30-
}).parse();
31-
3216
// Build the chat model
3317
function buildChain(chat: ChatOpenAI, systemMessage: string): LLMChain {
3418
const chatPrompt = ChatPromptTemplate.fromPromptMessages([

0 commit comments

Comments
 (0)