Skip to content

Commit ab58f5f

Browse files
committed
feat(hyper-link): the callback function adds more params (value, input, from, to) => {}.
1 parent 4d9890c commit ab58f5f

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

extensions/hyper-link/README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const hyperLink: Extension = [
6363
hyperLinkExtension({
6464
regexp: /Hyper/gi,
6565
match: { Hyper: 'https://google.com' },
66-
handle: (value) => {
66+
handle: (value, input, from, to) => {
6767
if (value === 'Hyper') return 'https://google.com';
6868
return value;
6969
},
@@ -82,6 +82,31 @@ const view = new EditorView({
8282
});
8383
```
8484

85+
## API
86+
87+
```ts
88+
import { ViewPlugin, DecorationSet, MatchDecorator, ViewUpdate } from '@codemirror/view';
89+
import { Extension } from '@codemirror/state';
90+
export interface HyperLinkState {
91+
at: number;
92+
url: string;
93+
anchor: HyperLinkExtensionOptions['anchor'];
94+
}
95+
export type HyperLinkExtensionOptions = {
96+
regexp?: RegExp;
97+
match?: Record<string, string>;
98+
handle?: (value: string, input: string, from: number, to: number) => string;
99+
anchor?: (dom: HTMLAnchorElement) => HTMLAnchorElement;
100+
};
101+
export declare function hyperLinkExtension({ regexp, match, handle, anchor }?: HyperLinkExtensionOptions): ViewPlugin<{
102+
decorator?: MatchDecorator | undefined;
103+
decorations: DecorationSet;
104+
update(update: ViewUpdate): void;
105+
}>;
106+
export declare const hyperLinkStyle: Extension;
107+
export declare const hyperLink: Extension;
108+
```
109+
85110
## Contributors
86111

87112
As always, thanks to our amazing contributors!

extensions/hyper-link/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ function hyperLinkDecorations(view: EditorView, anchor?: HyperLinkExtensionOptio
6767
const linkDecorator = (
6868
regexp?: RegExp,
6969
matchData?: Record<string, string>,
70-
matchFn?: (str: string) => string,
70+
matchFn?: (str: string, input: string, from: number, to: number) => string,
7171
anchor?: HyperLinkExtensionOptions['anchor'],
7272
) =>
7373
new MatchDecorator({
7474
regexp: regexp || /\b((?:https?|ftp):\/\/[^\s/$.?#].[^\s]*)\b/gi,
7575
decorate: (add, from, to, match, view) => {
7676
const url = match[0];
77-
let urlStr = matchFn && typeof matchFn === 'function' ? matchFn(url) : url;
77+
let urlStr = matchFn && typeof matchFn === 'function' ? matchFn(url, match.input, from, to) : url;
7878
if (matchData && matchData[url]) {
7979
urlStr = matchData[url];
8080
}
@@ -88,7 +88,7 @@ const linkDecorator = (
8888
export type HyperLinkExtensionOptions = {
8989
regexp?: RegExp;
9090
match?: Record<string, string>;
91-
handle?: (value: string) => string;
91+
handle?: (value: string, input: string, from: number, to: number) => string;
9292
anchor?: (dom: HTMLAnchorElement) => HTMLAnchorElement;
9393
};
9494

www/src/pages/extensions/hyper-link/example.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { hyperLink } from '@uiw/codemirror-extensions-hyper-link';
2+
// import { hyperLinkExtension, hyperLinkStyle } from '@uiw/codemirror-extensions-hyper-link';
23
import CodeMirror from '@uiw/react-codemirror';
34
import { langs } from '@uiw/codemirror-extensions-langs';
45
import { useTheme } from '../../../utils/useTheme';
56
import { markdownString } from './codeSample';
67
import { PageWarpper } from '..';
78

9+
// const hyperLink = [
10+
// hyperLinkExtension({
11+
// regexp: /codemirror/gi,
12+
// match: { codemirror: 'https://google.com' },
13+
// handle: (value, input, from, to) => {
14+
// if (value === 'Hyper') return 'https://google.com';
15+
// return value;
16+
// },
17+
// }),
18+
// hyperLinkStyle,
19+
// ];
20+
821
export const HyperLinkExample = () => {
922
const { theme } = useTheme();
1023

0 commit comments

Comments
 (0)