-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* linkify urls * update readme * handle settings upgrade
- Loading branch information
1 parent
7073fdb
commit 6f700ed
Showing
28 changed files
with
427 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import classNames from "classnames"; | ||
import { Dispatch, FormEvent } from "react"; | ||
|
||
export type CheckboxProps = Props<{ | ||
setChecked: Dispatch<boolean>; | ||
checked?: boolean; | ||
}>; | ||
|
||
export function Checkbox({ | ||
setChecked, | ||
checked, | ||
className, | ||
}: CheckboxProps): JSX.Element { | ||
const setNewChecked = (e: FormEvent<HTMLInputElement>) => { | ||
const newChecked = (e.target as HTMLInputElement).checked; | ||
setChecked(newChecked); | ||
}; | ||
|
||
return ( | ||
<input | ||
type="checkbox" | ||
className={classNames( | ||
"pl-1 dark:bg-gray-500 focus:outline-none cursor-pointer", | ||
className | ||
)} | ||
onChange={setNewChecked} | ||
checked={checked} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
extension/src/viewer/components/RenderedText/HighlightedText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ReactElement } from "react"; | ||
import { uid } from "uid"; | ||
import { Match } from "./Match"; | ||
|
||
export const SEARCH_TYPE = "search"; | ||
|
||
export interface SearchMatch extends Match<EmptyObject> { | ||
type: typeof SEARCH_TYPE; | ||
} | ||
|
||
// Rendering | ||
|
||
export function HighlightedText({ | ||
children, | ||
}: BaseProps): ReactElement<HTMLElement> { | ||
return <mark>{children}</mark>; | ||
} | ||
|
||
// Matching | ||
|
||
export type SearchOptions = Props<{ | ||
searchText: string; | ||
caseSensitive?: boolean; | ||
}>; | ||
|
||
export function matchSearch( | ||
text: string, | ||
{ searchText, caseSensitive }: SearchOptions | ||
): SearchMatch[] { | ||
const flags = "g" + (caseSensitive ? "" : "i"); | ||
const matches = text.matchAll(new RegExp(escapeRegExp(searchText), flags)); | ||
|
||
return Array.from(matches, (match) => ({ | ||
id: uid(), | ||
start: match.index!, | ||
end: match.index! + searchText.length, | ||
type: SEARCH_TYPE, | ||
metadata: {}, | ||
})); | ||
} | ||
|
||
// Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
function escapeRegExp(text: string): string { | ||
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
} |
68 changes: 68 additions & 0 deletions
68
extension/src/viewer/components/RenderedText/LinkifiedText.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import anchorme from "anchorme"; | ||
import type { ListingProps } from "anchorme/dist/node/types"; | ||
import { ReactElement } from "react"; | ||
import { uid } from "uid"; | ||
import { Match } from "./Match"; | ||
|
||
export const LINK_TYPE = "link"; | ||
|
||
// Rendering | ||
|
||
export type LinkMetadata = { | ||
linkType: "url" | "email"; | ||
href: string; | ||
}; | ||
|
||
export interface LinkMatch extends Match<LinkMetadata> { | ||
type: typeof LINK_TYPE; | ||
} | ||
|
||
export function LinkifiedText({ | ||
children, | ||
href, | ||
linkType, | ||
}: Props<LinkMetadata>): ReactElement<HTMLElement> { | ||
return ( | ||
<a | ||
href={href} | ||
target={linkType === "email" ? undefined : "_blank"} | ||
rel="noreferrer" | ||
className="underline" | ||
> | ||
{children} | ||
</a> | ||
); | ||
} | ||
|
||
// Matching | ||
|
||
export function matchLinks(text: string): LinkMatch[] { | ||
return anchorme | ||
.list(text) | ||
.filter((match) => getHref(match) !== null) | ||
.map((match) => ({ | ||
id: uid(), | ||
start: match.start, | ||
end: match.end, | ||
type: LINK_TYPE, | ||
metadata: { | ||
linkType: match.isEmail ? "email" : "url", | ||
href: getHref(match)!, | ||
}, | ||
})); | ||
} | ||
|
||
function getHref(match: ListingProps): Nullable<string> { | ||
// already has the protocol | ||
if (match.protocol) { | ||
return match.string; | ||
} | ||
|
||
// set default protocol for emails | ||
if (match.isEmail) { | ||
return "mailto:" + match.string; | ||
} | ||
|
||
// ignore url matches without protocol to avoid false positives | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Match<Metadata> { | ||
id: string; | ||
start: number; | ||
end: number; | ||
type: string; | ||
metadata: Metadata; | ||
} |
Oops, something went wrong.