Skip to content

Commit

Permalink
feat(hyper-link): the callback function adds more params `(value, inp…
Browse files Browse the repository at this point in the history
…ut, from, to) => {}`.
  • Loading branch information
jaywcjlove committed Jun 26, 2023
1 parent 4d9890c commit ab58f5f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
27 changes: 26 additions & 1 deletion extensions/hyper-link/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const hyperLink: Extension = [
hyperLinkExtension({
regexp: /Hyper/gi,
match: { Hyper: 'https://google.com' },
handle: (value) => {
handle: (value, input, from, to) => {
if (value === 'Hyper') return 'https://google.com';
return value;
},
Expand All @@ -82,6 +82,31 @@ const view = new EditorView({
});
```

## API

```ts
import { ViewPlugin, DecorationSet, MatchDecorator, ViewUpdate } from '@codemirror/view';
import { Extension } from '@codemirror/state';
export interface HyperLinkState {
at: number;
url: string;
anchor: HyperLinkExtensionOptions['anchor'];
}
export type HyperLinkExtensionOptions = {
regexp?: RegExp;
match?: Record<string, string>;
handle?: (value: string, input: string, from: number, to: number) => string;
anchor?: (dom: HTMLAnchorElement) => HTMLAnchorElement;
};
export declare function hyperLinkExtension({ regexp, match, handle, anchor }?: HyperLinkExtensionOptions): ViewPlugin<{
decorator?: MatchDecorator | undefined;
decorations: DecorationSet;
update(update: ViewUpdate): void;
}>;
export declare const hyperLinkStyle: Extension;
export declare const hyperLink: Extension;
```

## Contributors

As always, thanks to our amazing contributors!
Expand Down
6 changes: 3 additions & 3 deletions extensions/hyper-link/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ function hyperLinkDecorations(view: EditorView, anchor?: HyperLinkExtensionOptio
const linkDecorator = (
regexp?: RegExp,
matchData?: Record<string, string>,
matchFn?: (str: string) => string,
matchFn?: (str: string, input: string, from: number, to: number) => string,
anchor?: HyperLinkExtensionOptions['anchor'],
) =>
new MatchDecorator({
regexp: regexp || /\b((?:https?|ftp):\/\/[^\s/$.?#].[^\s]*)\b/gi,
decorate: (add, from, to, match, view) => {
const url = match[0];
let urlStr = matchFn && typeof matchFn === 'function' ? matchFn(url) : url;
let urlStr = matchFn && typeof matchFn === 'function' ? matchFn(url, match.input, from, to) : url;
if (matchData && matchData[url]) {
urlStr = matchData[url];
}
Expand All @@ -88,7 +88,7 @@ const linkDecorator = (
export type HyperLinkExtensionOptions = {
regexp?: RegExp;
match?: Record<string, string>;
handle?: (value: string) => string;
handle?: (value: string, input: string, from: number, to: number) => string;
anchor?: (dom: HTMLAnchorElement) => HTMLAnchorElement;
};

Expand Down
13 changes: 13 additions & 0 deletions www/src/pages/extensions/hyper-link/example.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { hyperLink } from '@uiw/codemirror-extensions-hyper-link';
// import { hyperLinkExtension, hyperLinkStyle } from '@uiw/codemirror-extensions-hyper-link';
import CodeMirror from '@uiw/react-codemirror';
import { langs } from '@uiw/codemirror-extensions-langs';
import { useTheme } from '../../../utils/useTheme';
import { markdownString } from './codeSample';
import { PageWarpper } from '..';

// const hyperLink = [
// hyperLinkExtension({
// regexp: /codemirror/gi,
// match: { codemirror: 'https://google.com' },
// handle: (value, input, from, to) => {
// if (value === 'Hyper') return 'https://google.com';
// return value;
// },
// }),
// hyperLinkStyle,
// ];

export const HyperLinkExample = () => {
const { theme } = useTheme();

Expand Down

1 comment on commit ab58f5f

@jaywcjlove
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.