@@ -4,6 +4,7 @@ import { wrapChildren } from "./to-estree"
44import { annotationsMap } from "../mdx-client/annotations"
55import { JsxNode as JsxNode , SuperNode } from "./nodes"
66import { getCommentData } from "./comment-data"
7+ import { CodeHikeConfig } from "./config"
78
89export function getAnnotationsFromMetastring (
910 options : Record < string , string >
@@ -18,7 +19,10 @@ export function getAnnotationsFromMetastring(
1819 return annotations
1920}
2021
21- export function extractAnnotationsFromCode ( code : Code ) {
22+ export function extractAnnotationsFromCode (
23+ code : Code ,
24+ config : CodeHikeConfig
25+ ) {
2226 const { lines } = code
2327 let lineNumber = 1
2428 const annotations = [ ] as CodeAnnotation [ ]
@@ -50,9 +54,48 @@ export function extractAnnotationsFromCode(code: Code) {
5054 lineNumber ++
5155 }
5256 }
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+ }
5380 return [ annotations , focusList . join ( "," ) ] as const
5481}
5582
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+
5699export function extractJSXAnnotations (
57100 node : SuperNode ,
58101 index : number ,
0 commit comments