File tree Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Expand file tree Collapse file tree 3 files changed +33
-4
lines changed Original file line number Diff line number Diff line change
1
+ console . log ( "one" )
2
+ console . log ( "two" )
3
+ console . log ( "three" )
4
+ console . log ( "four" )
5
+ console . log ( "five" )
6
+ console . log ( "six" )
7
+ console . log ( "seven" )
Original file line number Diff line number Diff line change 2
2
// from ./assets/foo.js
3
3
```
4
4
5
+ ``` js bar.js
6
+ // from ./assets/bar.js 3:5
7
+ ```
8
+
5
9
``` py foo.py
6
10
# from ./assets/foo.py
7
11
```
Original file line number Diff line number Diff line change @@ -212,7 +212,9 @@ async function getCodeFromExternalFileIfNeeded(
212
212
. map ( t => t . content )
213
213
. join ( "" )
214
214
215
- const codepath = commentData . data
215
+ const [ codepath , range ] = commentData . data
216
+ ?. trim ( )
217
+ . split ( / \s + / )
216
218
217
219
let fs , path
218
220
@@ -242,18 +244,34 @@ Looks like node "fs" and "path" modules are not available.`
242
244
const dir = path . dirname ( config . filepath )
243
245
const absoluteCodepath = path . resolve ( dir , codepath )
244
246
245
- let nodeValue
247
+ let content : string
246
248
try {
247
- nodeValue = fs . readFileSync ( absoluteCodepath , "utf8" )
249
+ content = fs . readFileSync ( absoluteCodepath , "utf8" )
248
250
} catch ( e ) {
249
251
e . message = `Code Hike couldn't resolve this annotation:
250
252
${ fileText }
251
253
${ absoluteCodepath } doesn't exist.`
252
254
throw e
253
255
}
254
256
257
+ if ( range ) {
258
+ const [ start , end ] = range . split ( ":" )
259
+ const startLine = parseInt ( start )
260
+ const endLine = parseInt ( end )
261
+ if ( isNaN ( startLine ) || isNaN ( endLine ) ) {
262
+ throw new Error (
263
+ `Code Hike couldn't resolve this annotation:
264
+ ${ fileText }
265
+ The range is not valid. Should be something like:
266
+ ${ codepath } 2:5`
267
+ )
268
+ }
269
+ const lines = content . split ( "\n" )
270
+ content = lines . slice ( startLine - 1 , endLine ) . join ( "\n" )
271
+ }
272
+
255
273
return await highlight ( {
256
- code : nodeValue ,
274
+ code : content ,
257
275
lang : code . lang ,
258
276
theme : config . theme ,
259
277
} )
You can’t perform that action at this time.
0 commit comments