@@ -59,57 +59,56 @@ export async function highlight(block: DocumentBlockCode): Promise<HighlightLine
59
59
// }
60
60
61
61
return runner . runBlocking ( async ( ) => {
62
- console . log ( `start ${ block . key } ` )
62
+ const start = Date . now ( ) ;
63
+ console . log ( `start ${ block . key } ${ tokenCount } tokens ${ lineCount } lines` )
63
64
const inlines : InlineIndexed [ ] = [ ] ;
64
65
const code = getPlainCodeBlock ( block , inlines ) ;
65
66
66
67
inlines . sort ( ( a , b ) => {
67
68
return a . start - b . start ;
68
69
} ) ;
69
70
70
- const highlighter = await loadHighlighter ( tokenCount > 4000 ) ;
71
+ const highlighter = await loadHighlighter ( ) ;
71
72
await loadHighlighterLanguage ( highlighter , langName ) ;
72
73
lineCount += block . nodes . length ;
73
74
74
- const start = Date . now ( ) ;
75
+
75
76
const lines = highlighter . codeToTokensBase ( code , {
76
77
lang : langName ,
77
78
tokenizeMaxLineLength : 120 ,
78
79
} ) ;
79
- const duration = Date . now ( ) - start ;
80
80
81
81
let currentIndex = 0 ;
82
- const result = plainHighlighting ( block ) ;
83
- // const result = lines.map((tokens, index) => {
84
- // tokenCount += tokens.length;
85
- // const lineBlock = block.nodes[index];
86
- // const result: HighlightToken[] = [];
87
-
88
- // const eatToken = (): PositionedToken | null => {
89
- // const token = tokens.shift();
90
- // if (token) {
91
- // currentIndex += token.content.length;
92
- // }
93
- // return token
94
- // ? { ...token, start: currentIndex - token.content.length, end: currentIndex }
95
- // : null;
96
- // };
97
-
98
- // while (tokens.length > 0) {
99
- // result.push(...matchTokenAndInlines(eatToken, inlines));
100
- // }
101
-
102
- // currentIndex += 1; // for the \n
103
-
104
- // return {
105
- // highlighted: !!lineBlock.data.highlighted,
106
- // tokens: result,
107
- // };
108
- // });
82
+ const result = lines . map ( ( tokens , index ) => {
83
+ tokenCount += tokens . length ;
84
+ const lineBlock = block . nodes [ index ] ;
85
+ const result : HighlightToken [ ] = [ ] ;
86
+
87
+ const eatToken = ( ) : PositionedToken | null => {
88
+ const token = tokens . shift ( ) ;
89
+ if ( token ) {
90
+ currentIndex += token . content . length ;
91
+ }
92
+ return token
93
+ ? { ...token , start : currentIndex - token . content . length , end : currentIndex }
94
+ : null ;
95
+ } ;
96
+
97
+ while ( tokens . length > 0 ) {
98
+ result . push ( ...matchTokenAndInlines ( eatToken , inlines ) ) ;
99
+ }
109
100
110
-
101
+ currentIndex += 1 ; // for the \n
102
+
103
+ return {
104
+ highlighted : ! ! lineBlock . data . highlighted ,
105
+ tokens : result ,
106
+ } ;
107
+ } ) ;
108
+
109
+ const duration = Date . now ( ) - start ;
111
110
console . log (
112
- `end ${ block . key } ${ duration } ms code len: ${ code . length } lineCountBefore: ${ lineCount } tokenCount: ${ tokenCount } created lines: ${ result . length } ` ,
111
+ `end ${ block . key } ${ duration } ms code len: ${ code . length } lineCountBefore: ${ lineCount } tokenCount: ${ tokenCount } created lines: ${ result . length } langs: ${ highlighter . getLoadedLanguages ( ) . join ( ',' ) } ` ,
113
112
) ;
114
113
return result ;
115
114
} ) ;
0 commit comments