File tree Expand file tree Collapse file tree 6 files changed +90
-39
lines changed
Expand file tree Collapse file tree 6 files changed +90
-39
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change 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" ,
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 }
Original file line number Diff line number Diff line change 11import { extname } from 'path' ;
22import json from './json' ;
33import yaml from './yaml' ;
4+ import markdown from './markdown' ;
45
56export type FormatterType = {
67 type : 'json' ,
78 formatter : json
89} | {
910 type : 'yaml' ,
1011 formatter : yaml
12+ } | {
13+ type : 'markdown' ,
14+ formatter : markdown
1115} ;
1216
1317export 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1- import yargs from 'yargs' ;
2- import { hideBin } from 'yargs/helpers' ;
31import { ChatOpenAI } from 'langchain/chat_models/openai' ;
42import {
53 ChatPromptTemplate ,
@@ -15,20 +13,6 @@ import ora from 'ora';
1513import languageEncoding from 'detect-file-encoding-and-language' ;
1614import 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
3317function buildChain ( chat : ChatOpenAI , systemMessage : string ) : LLMChain {
3418 const chatPrompt = ChatPromptTemplate . fromPromptMessages ( [
You can’t perform that action at this time.
0 commit comments