Skip to content

Commit 345ae60

Browse files
committed
Add config option
1 parent 1004f75 commit 345ae60

File tree

7 files changed

+39
-19
lines changed

7 files changed

+39
-19
lines changed

packages/mdx/dev/content/autolink.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{/* prettier-ignore */}
21
```py
32
import requests
43

@@ -8,6 +7,8 @@ urls = ["https://typicode.com/todos/1", "https://typicode.com/todos/2"]
87
responses = [requests.get(url) for url in urls]
98
```
109

10+
<CH.Code lineNumbers={true} autoLink={false}>
11+
1112
```ruby mark=3:5
1213
require 'net/http'
1314

@@ -17,3 +18,5 @@ url = URI.parse 'https://www.ruby-lang.org/en/'asdasd;
1718
response = Net::HTTP.get_response(url)
1819
puts response.body
1920
```
21+
22+
</CH.Code>

packages/mdx/dev/files.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function getCode(file: any, config = {}) {
3636
autoImport: false,
3737
skipLanguages: ["", "mermaid"],
3838
showCopyButton: true,
39+
autoLink: true,
3940
theme,
4041
...config,
4142
},

packages/mdx/src/remark/annotations.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { wrapChildren } from "./to-estree"
44
import { annotationsMap } from "../mdx-client/annotations"
55
import { JsxNode as JsxNode, SuperNode } from "./nodes"
66
import { getCommentData } from "./comment-data"
7+
import { CodeHikeConfig } from "./config"
78

89
export 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[]
@@ -52,24 +56,26 @@ export function extractAnnotationsFromCode(code: Code) {
5256
}
5357

5458
// extract links
55-
lineNumber = 1
56-
while (lineNumber <= lines.length) {
57-
const line = lines[lineNumber - 1]
58-
const lineContent = line.tokens
59-
.map(t => t.content)
60-
.join("")
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("")
6166

62-
const urls = extractURLsFromLine(lineContent)
63-
urls.forEach(({ url, start, end }) => {
64-
const Component = annotationsMap["link"]
65-
const focus = `${lineNumber}[${start + 1}:${end}]`
66-
annotations.push({
67-
Component,
68-
focus,
69-
data: url,
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+
})
7076
})
71-
})
72-
lineNumber++
77+
lineNumber++
78+
}
7379
}
7480
return [annotations, focusList.join(",")] as const
7581
}

packages/mdx/src/remark/code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async function mapFile(
130130
code = await getCodeFromExternalFileIfNeeded(code, config)
131131

132132
const [commentAnnotations, commentFocus] =
133-
extractAnnotationsFromCode(code)
133+
extractAnnotationsFromCode(code, config)
134134

135135
const options = parseMetastring(
136136
typeof node.meta === "string" ? node.meta : ""

packages/mdx/src/remark/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export type CodeHikeConfig = {
55
skipLanguages: string[]
66
showExpandButton?: boolean
77
showCopyButton?: boolean
8+
autoLink?: boolean
89
staticMediaQuery?: string
910
// path to the current file, internal use only
1011
filepath?: string

playground/src/app.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function App() {
2929
config: {
3030
lineNumbers: false,
3131
showCopyButton: false,
32+
autoLink: false,
3233
theme: "material-darker",
3334
},
3435
};

playground/src/editor.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ function ConfigEditor({ config, onChange }) {
104104
/>
105105
Copy Button
106106
</label>
107+
<label>
108+
<input
109+
type="checkbox"
110+
checked={config.autoLink}
111+
onChange={(e) => onChange({ ...config, autoLink: e.target.checked })}
112+
/>
113+
Auto Link
114+
</label>
107115
<label>
108116
Theme:
109117
<br />

0 commit comments

Comments
 (0)