Skip to content

Commit 567aadb

Browse files
committed
Annotation extractor function (fix #26)
1 parent 677d2d8 commit 567aadb

12 files changed

+483
-15
lines changed

.changeset/clever-cheetahs-fry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@code-hike/lighter": minor
3+
---
4+
5+
Add annotation extractor function

lib/dist/browser.esm.mjs

+1-1
Large diffs are not rendered by default.

lib/dist/index.cjs.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dist/index.d.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type Annotation = {
4848
query?: string;
4949
ranges: CodeRange[];
5050
};
51+
type AnnotationData = {
52+
name: string;
53+
rangeString: string;
54+
query?: string;
55+
};
56+
type AnnotationExtractor = string[] | ((comment: string) => null | AnnotationData);
5157

5258
type Token = {
5359
content: string;
@@ -113,7 +119,7 @@ declare function highlight(code: string, lang: LanguageAlias, themeOrThemeName?:
113119
declare function highlight(code: string, lang: LanguageAlias, themeOrThemeName: Theme, config: AnnotatedConfig): Promise<AnnotatedLighterResult>;
114120
declare function highlightSync(code: string, lang: LanguageAlias, themeOrThemeName?: Theme, config?: Config): LighterResult;
115121
declare function highlightSync(code: string, lang: LanguageAlias, themeOrThemeName: Theme, config: AnnotatedConfig): AnnotatedLighterResult;
116-
declare function extractAnnotations(code: string, lang: LanguageAlias, annotationNames?: string[]): Promise<{
122+
declare function extractAnnotations(code: string, lang: LanguageAlias, annotationExtractor?: AnnotationExtractor): Promise<{
117123
code: string;
118124
annotations: Annotation[];
119125
}>;

lib/dist/index.esm.mjs

+1-1
Large diffs are not rendered by default.

lib/dist/worker.esm.mjs

+1-1
Large diffs are not rendered by default.

lib/src/comments.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,21 @@ export type Annotation = {
3232
ranges: CodeRange[];
3333
};
3434

35+
export type AnnotationData = {
36+
name: string;
37+
rangeString: string;
38+
query?: string;
39+
};
40+
41+
export type AnnotationExtractor =
42+
| string[]
43+
| ((comment: string) => null | AnnotationData);
44+
3545
export function extractCommentsFromCode(
3646
code: string,
3747
grammar: IGrammar,
3848
lang: string,
39-
annotationNames: string[]
49+
annotationExtractor: AnnotationExtractor
4050
) {
4151
const lines = !grammar
4252
? highlightText(code)
@@ -49,7 +59,7 @@ export function extractCommentsFromCode(
4959
.map((line) => {
5060
const { annotations, lineWithoutComments } = getAnnotationsFromLine(
5161
line,
52-
annotationNames,
62+
annotationExtractor,
5363
lineNumber
5464
);
5565

@@ -81,7 +91,7 @@ export function extractCommentsFromCode(
8191

8292
function getAnnotationsFromLine(
8393
tokens: Token[],
84-
names: string[],
94+
annotationExtractor: AnnotationExtractor,
8595
lineNumber: number
8696
) {
8797
// if no punctuation return empty
@@ -106,12 +116,17 @@ function getAnnotationsFromLine(
106116
continue;
107117
}
108118

109-
const { name, query, rangeString } = getAnnotationData(token.content);
110-
if (!names.includes(name)) {
119+
const annotationData =
120+
typeof annotationExtractor === "function"
121+
? annotationExtractor(token.content)
122+
: getAnnotationDataFromNames(token.content, annotationExtractor);
123+
124+
if (!annotationData) {
111125
// a comment, but not an annotation
112126
i++;
113127
continue;
114128
}
129+
const { name, query, rangeString } = annotationData;
115130

116131
// we have an annotation
117132
const prevToken = tokens[i - 1];
@@ -156,11 +171,16 @@ function getAnnotationsFromLine(
156171
};
157172
}
158173

159-
function getAnnotationData(content: string) {
174+
function getAnnotationDataFromNames(content: string, names: string[]) {
160175
const regex = /\s*([\w-]+)?(\([^\)]*\)|\[[^\]]*\])?(.*)$/;
161176
const match = content.match(regex);
162177
const name = match[1];
163178
const rangeString = match[2];
164179
const query = match[3]?.trim();
180+
181+
if (!names.includes(name)) {
182+
return null;
183+
}
184+
165185
return { name, rangeString, query };
166186
}

lib/src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
preloadGrammars,
1818
highlightText,
1919
} from "./highlighter";
20-
import { Annotation, extractCommentsFromCode } from "./comments";
20+
import {
21+
Annotation,
22+
AnnotationExtractor,
23+
extractCommentsFromCode,
24+
} from "./comments";
2125
import {
2226
applyAnnotations,
2327
Lines,
@@ -174,9 +178,9 @@ export function highlightSync(
174178
export async function extractAnnotations(
175179
code: string,
176180
lang: LanguageAlias,
177-
annotationNames: string[] = []
181+
annotationExtractor?: AnnotationExtractor
178182
) {
179-
if (annotationNames.length === 0) {
183+
if (!annotationExtractor) {
180184
return { code, annotations: [] };
181185
}
182186

@@ -187,7 +191,7 @@ export async function extractAnnotations(
187191
code,
188192
grammar,
189193
lang,
190-
annotationNames
194+
annotationExtractor
191195
);
192196

193197
return { code: newCode, annotations };

lib/test/__snapshots__/browser.test.ts.snap

+134
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,140 @@ exports[`extract annottations from txt 2`] = `
430430
}
431431
`;
432432

433+
exports[`extract annottations with prefix 1`] = `
434+
{
435+
"annotations": [
436+
{
437+
"name": "xy",
438+
"query": "bar",
439+
"ranges": [
440+
{
441+
"fromColumn": 3,
442+
"lineNumber": 3,
443+
"toColumn": 5,
444+
},
445+
],
446+
},
447+
],
448+
"code": "// xyz[3:5] foo
449+
const x = 1;
450+
const y = 2;",
451+
}
452+
`;
453+
454+
exports[`extract annottations with prefix 2`] = `
455+
{
456+
"lang": "javascript",
457+
"lines": [
458+
{
459+
"lineNumber": 1,
460+
"tokens": [
461+
{
462+
"content": "// xyz[3:5] foo",
463+
"style": {
464+
"color": "#6A9955",
465+
},
466+
},
467+
],
468+
},
469+
{
470+
"lineNumber": 2,
471+
"tokens": [
472+
{
473+
"content": "const ",
474+
"style": {
475+
"color": "#569CD6",
476+
},
477+
},
478+
{
479+
"content": "x",
480+
"style": {
481+
"color": "#4FC1FF",
482+
},
483+
},
484+
{
485+
"content": " = ",
486+
"style": {
487+
"color": "#D4D4D4",
488+
},
489+
},
490+
{
491+
"content": "1",
492+
"style": {
493+
"color": "#B5CEA8",
494+
},
495+
},
496+
{
497+
"content": ";",
498+
"style": {
499+
"color": "#D4D4D4",
500+
},
501+
},
502+
],
503+
},
504+
{
505+
"lineNumber": 3,
506+
"tokens": [
507+
{
508+
"content": "co",
509+
"style": {
510+
"color": "#569CD6",
511+
},
512+
},
513+
{
514+
"annotationName": "xy",
515+
"annotationQuery": "bar",
516+
"fromColumn": 3,
517+
"toColumn": 5,
518+
"tokens": [
519+
{
520+
"content": "nst",
521+
"style": {
522+
"color": "#569CD6",
523+
},
524+
},
525+
],
526+
},
527+
{
528+
"content": " ",
529+
"style": {
530+
"color": "#569CD6",
531+
},
532+
},
533+
{
534+
"content": "y",
535+
"style": {
536+
"color": "#4FC1FF",
537+
},
538+
},
539+
{
540+
"content": " = ",
541+
"style": {
542+
"color": "#D4D4D4",
543+
},
544+
},
545+
{
546+
"content": "2",
547+
"style": {
548+
"color": "#B5CEA8",
549+
},
550+
},
551+
{
552+
"content": ";",
553+
"style": {
554+
"color": "#D4D4D4",
555+
},
556+
},
557+
],
558+
},
559+
],
560+
"style": {
561+
"background": "#1E1E1E",
562+
"color": "#D4D4D4",
563+
},
564+
}
565+
`;
566+
433567
exports[`highlight html with theme 1`] = `
434568
{
435569
"lang": "html",

0 commit comments

Comments
 (0)