1- import * as CycleDOM from '@cycle/dom '
1+ import * as Preact from 'preact '
22import Logger from './Logger'
33
4- const highlightInfoListToOb = ( list : any ) => {
4+ export type HighlightInformation = {
5+ classes : Array < string >
6+ word : string
7+ description : string
8+ }
9+
10+ const highlightInfoListToOb = ( list : Array < any > ) => {
511 const obj : { [ key : string ] : any } = { }
612 for ( let x of list ) {
713 const key = x [ 0 ] . slice ( 1 )
@@ -13,7 +19,7 @@ const highlightInfoListToOb = (list: any) => {
1319
1420// Use the right CSS classes, so that we can use the
1521// syntax highlighting built into atom.
16- const decorToClasses = ( decor : any ) => {
22+ const decorToClasses = ( decor : string ) => {
1723 switch ( decor ) {
1824 case ':type' :
1925 return [ 'syntax--storage' , 'syntax--type' ]
@@ -36,7 +42,7 @@ const decorToClasses = (decor: any) => {
3642 }
3743}
3844
39- const highlightWord = ( word : string , info : any ) => {
45+ const highlightWord = ( word : string , info : any ) : HighlightInformation => {
4046 const type = info . info . type || ''
4147 const doc = info . info [ 'doc-overview' ] || ''
4248
@@ -51,22 +57,26 @@ const highlightWord = (word: string, info: any) => {
5157
5258// Build highlighting information that we can then pass to one
5359// of our serializers.
54- export const highlight = ( code : string , highlightingInfo : any ) => {
60+ export const highlight = (
61+ code : string ,
62+ highlightingInfo : Array < [ number , number , Array < any > ] > ,
63+ ) : Array < HighlightInformation > => {
5564 const highlighted = highlightingInfo
56- . map ( function ( [ start , length , info ] : [ any , any , any ] ) {
65+ . map ( function ( [ start , length , info ] ) {
5766 return {
5867 start,
5968 length,
6069 info : highlightInfoListToOb ( info ) ,
6170 }
6271 } )
63- . filter ( ( i : any ) => i . info . decor != null )
64- . reduce (
65- ( [ position , text ] : any , info : any ) => {
72+ . filter ( ( i ) => i . info . decor != null )
73+ . reduce < [ number , Array < HighlightInformation > ] > (
74+ ( [ position , text ] , info ) => {
6675 const newPosition = info . start + info . length
67- const unhighlightedText = {
76+ const unhighlightedText : HighlightInformation = {
6877 classes : [ ] ,
6978 word : code . slice ( position , info . start ) ,
79+ description : '' ,
7080 }
7181 const highlightedWord = highlightWord (
7282 code . slice ( info . start , newPosition ) ,
@@ -79,10 +89,11 @@ export const highlight = (code: string, highlightingInfo: any) => {
7989 [ 0 , [ ] ] ,
8090 )
8191
82- const [ position , text ] = Array . from ( highlighted )
92+ const [ position , text ] = highlighted
8393 const rest = {
8494 classes : [ ] ,
8595 word : code . slice ( position ) ,
96+ description : '' ,
8697 }
8798 const higlightedWords = text . concat ( rest )
8899 return higlightedWords . filter (
@@ -91,9 +102,9 @@ export const highlight = (code: string, highlightingInfo: any) => {
91102}
92103
93104// Applies the highlighting and returns the result as an html-string.
94- export const highlightToString = ( highlights : any ) =>
105+ export const highlightToString = ( highlights : Array < HighlightInformation > ) =>
95106 highlights
96- . map ( function ( { classes, word } : any ) {
107+ . map ( function ( { classes, word } ) {
97108 if ( classes . length === 0 ) {
98109 return word
99110 } else {
@@ -103,8 +114,8 @@ export const highlightToString = (highlights: any) =>
103114 . join ( '' )
104115
105116// Applies the highlighting and returns the result as a DOM-objects.
106- export const highlightToHtml = ( highlights : any ) => {
107- const spans = highlights . map ( function ( { classes, word } : any ) {
117+ export const highlightToHtml = ( highlights : Array < HighlightInformation > ) => {
118+ const spans = highlights . map ( function ( { classes, word } ) {
108119 if ( classes . length === 0 ) {
109120 return document . createTextNode ( word )
110121 } else {
@@ -119,15 +130,19 @@ export const highlightToHtml = (highlights: any) => {
119130 return container
120131}
121132
122- export const highlightToCycle = ( highlights : any ) =>
123- highlights . map ( ( { classes, word, description } : any ) => {
133+ export const highlightToPreact = (
134+ highlights : Array < HighlightInformation > ,
135+ ) : Preact . VNode => {
136+ const highlighted = highlights . map ( ( { classes, word, description } ) => {
124137 if ( classes . length === 0 ) {
125- return word
138+ return word as string
126139 } else {
127- return CycleDOM . h (
140+ return Preact . h (
128141 'span' ,
129142 { className : classes . join ( ' ' ) , title : description } ,
130143 word ,
131144 )
132145 }
133146 } )
147+ return Preact . h ( 'div' , { } , highlighted )
148+ }
0 commit comments