Skip to content

Commit c676a69

Browse files
committed
Fix #23
1 parent 5058c36 commit c676a69

File tree

9 files changed

+134
-68
lines changed

9 files changed

+134
-68
lines changed

.changeset/rich-comics-float.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@code-hike/lighter": patch
3+
---
4+
5+
Better mdx comment annotations
6+
7+
- remove `{}` after extracting the annotation

lib/dist/browser.esm.mjs

+15-9
Original file line numberDiff line numberDiff line change
@@ -2887,24 +2887,30 @@ const commentsTheme = {
28872887
// { scope: "comment.block", settings: { foreground: BLOCK_COMMENT } },
28882888
],
28892889
};
2890-
function extractCommentsFromCode(code, grammar, annotationNames) {
2890+
function extractCommentsFromCode(code, grammar, lang, annotationNames) {
28912891
const lines = !grammar
28922892
? highlightText(code)
28932893
: highlightTokens(code, grammar, commentsTheme);
28942894
const allAnnotations = [];
28952895
let lineNumber = 1;
2896-
const cleanLines = lines
2896+
const newCode = lines
28972897
.map((line) => {
28982898
const { annotations, lineWithoutComments } = getAnnotationsFromLine(line, annotationNames, lineNumber);
28992899
allAnnotations.push(...annotations);
2900-
if (lineWithoutComments) {
2901-
lineNumber++;
2900+
if (!lineWithoutComments) {
2901+
return null;
29022902
}
2903-
return lineWithoutComments;
2903+
const lineText = lineWithoutComments.map((t) => t.content).join("");
2904+
// remove mdx comment wrapper https://github.com/code-hike/lighter/issues/23
2905+
if (lang === "mdx" &&
2906+
annotations.length > 0 &&
2907+
lineText.trim() === "{}") {
2908+
return null;
2909+
}
2910+
lineNumber++;
2911+
return lineText;
29042912
})
2905-
.filter((line) => line !== null);
2906-
const newCode = cleanLines
2907-
.map((line) => line.map((t) => t.content).join(""))
2913+
.filter((line) => line !== null)
29082914
.join(`\n`);
29092915
return { newCode, annotations: allAnnotations };
29102916
}
@@ -3306,7 +3312,7 @@ async function extractAnnotations(code, lang, annotationNames = []) {
33063312
}
33073313
await preloadGrammars([lang]);
33083314
const { grammar } = getGrammar(lang);
3309-
const { newCode, annotations } = extractCommentsFromCode(code, grammar, annotationNames);
3315+
const { newCode, annotations } = extractCommentsFromCode(code, grammar, lang, annotationNames);
33103316
return { code: newCode, annotations };
33113317
}
33123318
async function getThemeColors(themeOrThemeName) {

lib/dist/index.cjs.js

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

lib/dist/index.esm.mjs

+15-9
Original file line numberDiff line numberDiff line change
@@ -3235,24 +3235,30 @@ const commentsTheme = {
32353235
// { scope: "comment.block", settings: { foreground: BLOCK_COMMENT } },
32363236
],
32373237
};
3238-
function extractCommentsFromCode(code, grammar, annotationNames) {
3238+
function extractCommentsFromCode(code, grammar, lang, annotationNames) {
32393239
const lines = !grammar
32403240
? highlightText(code)
32413241
: highlightTokens(code, grammar, commentsTheme);
32423242
const allAnnotations = [];
32433243
let lineNumber = 1;
3244-
const cleanLines = lines
3244+
const newCode = lines
32453245
.map((line) => {
32463246
const { annotations, lineWithoutComments } = getAnnotationsFromLine(line, annotationNames, lineNumber);
32473247
allAnnotations.push(...annotations);
3248-
if (lineWithoutComments) {
3249-
lineNumber++;
3248+
if (!lineWithoutComments) {
3249+
return null;
32503250
}
3251-
return lineWithoutComments;
3251+
const lineText = lineWithoutComments.map((t) => t.content).join("");
3252+
// remove mdx comment wrapper https://github.com/code-hike/lighter/issues/23
3253+
if (lang === "mdx" &&
3254+
annotations.length > 0 &&
3255+
lineText.trim() === "{}") {
3256+
return null;
3257+
}
3258+
lineNumber++;
3259+
return lineText;
32523260
})
3253-
.filter((line) => line !== null);
3254-
const newCode = cleanLines
3255-
.map((line) => line.map((t) => t.content).join(""))
3261+
.filter((line) => line !== null)
32563262
.join(`\n`);
32573263
return { newCode, annotations: allAnnotations };
32583264
}
@@ -3654,7 +3660,7 @@ async function extractAnnotations(code, lang, annotationNames = []) {
36543660
}
36553661
await preloadGrammars([lang]);
36563662
const { grammar } = getGrammar(lang);
3657-
const { newCode, annotations } = extractCommentsFromCode(code, grammar, annotationNames);
3663+
const { newCode, annotations } = extractCommentsFromCode(code, grammar, lang, annotationNames);
36583664
return { code: newCode, annotations };
36593665
}
36603666
async function getThemeColors(themeOrThemeName) {

lib/src/comments.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type Annotation = {
3535
export function extractCommentsFromCode(
3636
code: string,
3737
grammar: IGrammar,
38+
lang: string,
3839
annotationNames: string[]
3940
) {
4041
const lines = !grammar
@@ -44,7 +45,7 @@ export function extractCommentsFromCode(
4445
const allAnnotations: Annotation[] = [];
4546

4647
let lineNumber = 1;
47-
const cleanLines = lines
48+
const newCode = lines
4849
.map((line) => {
4950
const { annotations, lineWithoutComments } = getAnnotationsFromLine(
5051
line,
@@ -54,16 +55,25 @@ export function extractCommentsFromCode(
5455

5556
allAnnotations.push(...annotations);
5657

57-
if (lineWithoutComments) {
58-
lineNumber++;
58+
if (!lineWithoutComments) {
59+
return null;
5960
}
6061

61-
return lineWithoutComments;
62-
})
63-
.filter((line) => line !== null);
62+
const lineText = lineWithoutComments.map((t) => t.content).join("");
63+
64+
// remove mdx comment wrapper https://github.com/code-hike/lighter/issues/23
65+
if (
66+
lang === "mdx" &&
67+
annotations.length > 0 &&
68+
lineText.trim() === "{}"
69+
) {
70+
return null;
71+
}
6472

65-
const newCode = cleanLines
66-
.map((line) => line.map((t) => t.content).join(""))
73+
lineNumber++;
74+
return lineText;
75+
})
76+
.filter((line) => line !== null)
6777
.join(`\n`);
6878

6979
return { newCode, annotations: allAnnotations };

lib/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ export async function extractAnnotations(
181181
const { newCode, annotations } = extractCommentsFromCode(
182182
code,
183183
grammar,
184+
lang,
184185
annotationNames
185186
);
186187

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ exports[`extract annottations from mdx 1`] = `
124124
],
125125
},
126126
],
127-
"code": "{}
128-
# Hi there
127+
"code": "# Hi there
129128
{/* bar */}",
130129
}
131130
`;
@@ -138,18 +137,29 @@ exports[`extract annottations from mdx 2`] = `
138137
"lineNumber": 1,
139138
"tokens": [
140139
{
141-
"content": "{}",
140+
"content": "# ",
142141
"style": {
143-
"color": "#CE9178",
142+
"color": "#569CD6",
143+
"fontWeight": "bold",
144144
},
145145
},
146-
],
147-
},
148-
{
149-
"lineNumber": 2,
150-
"tokens": [
151146
{
152-
"content": "# Hi there",
147+
"annotationName": "foo",
148+
"annotationQuery": "",
149+
"fromColumn": 3,
150+
"toColumn": 5,
151+
"tokens": [
152+
{
153+
"content": "Hi ",
154+
"style": {
155+
"color": "#569CD6",
156+
"fontWeight": "bold",
157+
},
158+
},
159+
],
160+
},
161+
{
162+
"content": "there",
153163
"style": {
154164
"color": "#569CD6",
155165
"fontWeight": "bold",
@@ -158,7 +168,7 @@ exports[`extract annottations from mdx 2`] = `
158168
],
159169
},
160170
{
161-
"lineNumber": 3,
171+
"lineNumber": 2,
162172
"tokens": [
163173
{
164174
"content": "{",

lib/test/__snapshots__/cjs.test.ts.snap

+21-11
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ exports[`extract annottations from mdx 1`] = `
124124
],
125125
},
126126
],
127-
"code": "{}
128-
# Hi there
127+
"code": "# Hi there
129128
{/* bar */}",
130129
}
131130
`;
@@ -138,18 +137,29 @@ exports[`extract annottations from mdx 2`] = `
138137
"lineNumber": 1,
139138
"tokens": [
140139
{
141-
"content": "{}",
140+
"content": "# ",
142141
"style": {
143-
"color": "#CE9178",
142+
"color": "#569CD6",
143+
"fontWeight": "bold",
144144
},
145145
},
146-
],
147-
},
148-
{
149-
"lineNumber": 2,
150-
"tokens": [
151146
{
152-
"content": "# Hi there",
147+
"annotationName": "foo",
148+
"annotationQuery": "",
149+
"fromColumn": 3,
150+
"toColumn": 5,
151+
"tokens": [
152+
{
153+
"content": "Hi ",
154+
"style": {
155+
"color": "#569CD6",
156+
"fontWeight": "bold",
157+
},
158+
},
159+
],
160+
},
161+
{
162+
"content": "there",
153163
"style": {
154164
"color": "#569CD6",
155165
"fontWeight": "bold",
@@ -158,7 +168,7 @@ exports[`extract annottations from mdx 2`] = `
158168
],
159169
},
160170
{
161-
"lineNumber": 3,
171+
"lineNumber": 2,
162172
"tokens": [
163173
{
164174
"content": "{",

lib/test/__snapshots__/esm.test.ts.snap

+21-11
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ exports[`extract annottations from mdx 1`] = `
124124
],
125125
},
126126
],
127-
"code": "{}
128-
# Hi there
127+
"code": "# Hi there
129128
{/* bar */}",
130129
}
131130
`;
@@ -138,18 +137,29 @@ exports[`extract annottations from mdx 2`] = `
138137
"lineNumber": 1,
139138
"tokens": [
140139
{
141-
"content": "{}",
140+
"content": "# ",
142141
"style": {
143-
"color": "#CE9178",
142+
"color": "#569CD6",
143+
"fontWeight": "bold",
144144
},
145145
},
146-
],
147-
},
148-
{
149-
"lineNumber": 2,
150-
"tokens": [
151146
{
152-
"content": "# Hi there",
147+
"annotationName": "foo",
148+
"annotationQuery": "",
149+
"fromColumn": 3,
150+
"toColumn": 5,
151+
"tokens": [
152+
{
153+
"content": "Hi ",
154+
"style": {
155+
"color": "#569CD6",
156+
"fontWeight": "bold",
157+
},
158+
},
159+
],
160+
},
161+
{
162+
"content": "there",
153163
"style": {
154164
"color": "#569CD6",
155165
"fontWeight": "bold",
@@ -158,7 +168,7 @@ exports[`extract annottations from mdx 2`] = `
158168
],
159169
},
160170
{
161-
"lineNumber": 3,
171+
"lineNumber": 2,
162172
"tokens": [
163173
{
164174
"content": "{",

0 commit comments

Comments
 (0)