Skip to content

Commit 3037c50

Browse files
committed
feat: support TrustedValue
1 parent 5702ed9 commit 3037c50

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/mdEscape.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isTrustedValue, TrustedValue } from "./trusted";
2+
13
const replacementsTuples: [matchPattern: RegExp, newChar: string][] = [
24
[/\*/g, "\\*"],
35
[/#/g, "\\#"],
@@ -10,7 +12,10 @@ const replacementsTuples: [matchPattern: RegExp, newChar: string][] = [
1012
[/>/g, ">"],
1113
[/_/g, "\\_"]
1214
];
13-
export const mdEscape = (text: string): string => {
15+
export const mdEscape = (text: string | TrustedValue): string => {
16+
if (isTrustedValue(text)) {
17+
return text.value;
18+
}
1419
return replacementsTuples.reduce((text, replacement) => {
1520
return text.replace(replacement[0], replacement[1]);
1621
}, text);

src/mdImg.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { mdEscape } from "./mdEscape";
2+
import { TrustedValue } from "./trusted";
23

34
export type MdImgOptions = {
45
url: string;
5-
alt?: string;
6+
alt?: string | TrustedValue;
67
};
78
export const mdImg = ({ url, alt = "" }: MdImgOptions): string => {
89
return `![${mdEscape(alt)}](${url})`;

src/mdLink.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { mdEscape } from "./mdEscape";
2+
import { TrustedValue } from "./trusted";
23

34
export type MdLinkOptions = {
4-
text: string;
5+
text: string | TrustedValue;
56
url: string;
6-
title?: string;
7+
title?: string | TrustedValue;
78
};
89
export const mdLink = ({ text, url, title }: MdLinkOptions): string => {
910
if (title) {

src/trusted.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const TrustedSymbol = Symbol("Trusted Value");
2+
export type TrustedValue = {
3+
trustedSymbol: Symbol;
4+
value: string;
5+
};
6+
export const isTrustedValue = (value: any): value is TrustedValue => {
7+
return (
8+
typeof value === "object" &&
9+
value !== null &&
10+
"trustedSymbol" in value &&
11+
value["trustedSymbol"] === TrustedSymbol
12+
);
13+
};
14+
export const trusted = (str: string) => {
15+
return {
16+
trustedSymbol: TrustedSymbol,
17+
value: str
18+
};
19+
};

0 commit comments

Comments
 (0)