@@ -4,6 +4,7 @@ import { wrapChildren } from "./to-estree"
4
4
import { annotationsMap } from "../mdx-client/annotations"
5
5
import { JsxNode as JsxNode , SuperNode } from "./nodes"
6
6
import { getCommentData } from "./comment-data"
7
+ import { CodeHikeConfig } from "./config"
7
8
8
9
export function getAnnotationsFromMetastring (
9
10
options : Record < string , string >
@@ -18,7 +19,10 @@ export function getAnnotationsFromMetastring(
18
19
return annotations
19
20
}
20
21
21
- export function extractAnnotationsFromCode ( code : Code ) {
22
+ export function extractAnnotationsFromCode (
23
+ code : Code ,
24
+ config : CodeHikeConfig
25
+ ) {
22
26
const { lines } = code
23
27
let lineNumber = 1
24
28
const annotations = [ ] as CodeAnnotation [ ]
@@ -50,9 +54,48 @@ export function extractAnnotationsFromCode(code: Code) {
50
54
lineNumber ++
51
55
}
52
56
}
57
+
58
+ // extract links
59
+ if ( config . autoLink ) {
60
+ lineNumber = 1
61
+ while ( lineNumber <= lines . length ) {
62
+ const line = lines [ lineNumber - 1 ]
63
+ const lineContent = line . tokens
64
+ . map ( t => t . content )
65
+ . join ( "" )
66
+
67
+ const urls = extractURLsFromLine ( lineContent )
68
+ urls . forEach ( ( { url, start, end } ) => {
69
+ const Component = annotationsMap [ "link" ]
70
+ const focus = `${ lineNumber } [${ start + 1 } :${ end } ]`
71
+ annotations . push ( {
72
+ Component,
73
+ focus,
74
+ data : url ,
75
+ } )
76
+ } )
77
+ lineNumber ++
78
+ }
79
+ }
53
80
return [ annotations , focusList . join ( "," ) ] as const
54
81
}
55
82
83
+ const urlRegex = / h t t p s ? : \/ \/ [ \w \- _ . ~ : / ? # [ \] @ ! $ & * + , ; = % ] + / g
84
+ function extractURLsFromLine ( line : string ) {
85
+ const urls = [ ]
86
+ let match : RegExpExecArray | null
87
+
88
+ while ( ( match = urlRegex . exec ( line ) ) !== null ) {
89
+ const url = match [ 0 ]
90
+ const start = match . index
91
+ const end = start + url . length
92
+
93
+ urls . push ( { url, start, end } )
94
+ }
95
+
96
+ return urls
97
+ }
98
+
56
99
export function extractJSXAnnotations (
57
100
node : SuperNode ,
58
101
index : number ,
0 commit comments