-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderCore.ts
More file actions
136 lines (113 loc) · 4.04 KB
/
Copy pathrenderCore.ts
File metadata and controls
136 lines (113 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* renderCore.ts :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlesieur <dlesieur@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/05/18 21:19:17 by dlesieur #+# #+# */
/* Updated: 2026/05/18 21:19:17 by dlesieur ### ########.fr */
/* */
/* ************************************************************************** */
export type MarkdownViewMode = "source" | "live-preview" | "reading";
export type MarkdownModeResolver<TNode> = (
index: number,
node: TNode,
) => MarkdownViewMode | undefined;
export interface MarkdownModeState {
readonly name: MarkdownViewMode;
getBlockState(): MarkdownViewMode;
shouldHideMarkdownSymbols(): boolean;
}
export class SourceMode implements MarkdownModeState {
public readonly name: MarkdownViewMode = "source";
public getBlockState(): MarkdownViewMode {
return this.name;
}
public shouldHideMarkdownSymbols(): boolean {
return false;
}
}
export class LivePreviewMode implements MarkdownModeState {
public readonly name: MarkdownViewMode = "live-preview";
public getBlockState(): MarkdownViewMode {
return this.name;
}
public shouldHideMarkdownSymbols(): boolean {
return true;
}
}
export class ReadingMode implements MarkdownModeState {
public readonly name: MarkdownViewMode = "reading";
public getBlockState(): MarkdownViewMode {
return this.name;
}
public shouldHideMarkdownSymbols(): boolean {
return true;
}
}
const SOURCE_MODE = new SourceMode();
const LIVE_PREVIEW_MODE = new LivePreviewMode();
const READING_MODE = new ReadingMode();
export function resolveMarkdownMode(
mode?: MarkdownViewMode,
): MarkdownModeState {
switch (mode) {
case "source":
return SOURCE_MODE;
case "live-preview":
return LIVE_PREVIEW_MODE;
case "reading":
default:
return READING_MODE;
}
}
export function resolveIndexedMarkdownMode<TNode>(
fallbackMode: MarkdownViewMode | undefined,
index: number,
node: TNode,
indexedModes?: readonly MarkdownViewMode[],
resolver?: MarkdownModeResolver<TNode>,
): MarkdownModeState {
return resolveMarkdownMode(
resolver?.(index, node) ?? indexedModes?.[index] ?? fallbackMode,
);
}
const HTML_ESCAPE_PATTERN = /[&<>"']/g;
const HTML_ESCAPE_MAP: Record<string, string> = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
};
export function escapeHtml(value: string): string {
return value.replaceAll(HTML_ESCAPE_PATTERN, (char) => HTML_ESCAPE_MAP[char]);
}
export function isExternalUrl(href: string): boolean {
return /^https?:\/\//i.test(href.trim());
}
export function sanitizeUrl(value: string): string {
const trimmed = value.trim();
if (!trimmed) return "";
const normalized = stripUrlControlAndSpaceChars(trimmed);
const schemeMatch = /^([a-z][a-z\d+.-]*):/i.exec(normalized);
if (!schemeMatch) return trimmed;
const scheme = schemeMatch[1].toLowerCase();
// "page:" is osionos's own internal page-link scheme (see
// INTERNAL_PAGE_LINK_PREFIX in EditableContent.tsx) — a closed, app-minted
// vocabulary (a page id), not executable/arbitrary like javascript: etc.
if (scheme === "http" || scheme === "https" || scheme === "mailto" || scheme === "tel" || scheme === "page") {
return trimmed;
}
return "";
}
function stripUrlControlAndSpaceChars(value: string): string {
let result = "";
for (const char of value) {
const codePoint = char.codePointAt(0);
if (codePoint === undefined || codePoint <= 0x20 || codePoint === 0x7F) continue;
result += char;
}
return result;
}