@vetta-org/theme-ui 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +735 -7
- package/src/action-approval/AppearanceActionPickerView.tsx +2 -78
- package/src/action-approval/AppearanceApprovalDrawerView.tsx +2 -9
- package/src/action-approval/BatchTasksApprovalFrameView.tsx +4 -1
- package/src/action-approval/GenericActionApprovalView.tsx +2 -9
- package/src/action-approval/ManageActionApprovalFrameView.tsx +4 -1
- package/src/action-approval/NavigationOpenApprovalView.tsx +2 -9
- package/src/action-approval/SchedulerActionApprovalDialogView.tsx +3 -1
- package/src/action-approval/SchedulerEditApprovalDrawerView.tsx +3 -10
- package/src/action-approval/index.ts +0 -1
- package/src/activity/ActivityPanel.tsx +13 -10
- package/src/activity/ActivityPanelFrame.test.tsx +32 -0
- package/src/activity/ActivityPanelFrame.tsx +3 -2
- package/src/activity/BackgroundTasksTabPanelView.tsx +1 -1
- package/src/activity/BrowserPanelView.tsx +48 -49
- package/src/activity/DebugTabPanelView.tsx +1 -1
- package/src/activity/MarkdownPreviewView.tsx +56 -59
- package/src/activity/PluginTabPickerView.tsx +5 -3
- package/src/activity/PortsTabPanelView.tsx +911 -0
- package/src/activity/ToolCallsSubTabView.tsx +1 -1
- package/src/activity/WorkflowTabPanelView.tsx +1 -1
- package/src/activity/index.ts +9 -0
- package/src/app/AppBackground.tsx +1 -1
- package/src/app/AppBootLoadingView.tsx +4 -2
- package/src/app-shell/DefaultWindowControls.tsx +1 -1
- package/src/app-shell/PageHeaderActionGroup.tsx +1 -1
- package/src/app-shell/PageHeaderContent.tsx +36 -7
- package/src/app-shell/PageHeaderFrame.tsx +15 -3
- package/src/app-shell/PageHeaderSidebarTrigger.tsx +1 -1
- package/src/app-shell/PageHeaderTitle.tsx +1 -1
- package/src/app-shell/WindowControlButton.tsx +1 -1
- package/src/app-shell/header-crowding.test.ts +62 -0
- package/src/app-shell/header-crowding.tsx +137 -0
- package/src/appearance/BackgroundImageFrame.tsx +1 -1
- package/src/appearance/CenterSliceImageFrame.tsx +1 -1
- package/src/appearance/CornerImageFrame.tsx +1 -1
- package/src/appearance/HorizontalSliceImageFrame.tsx +1 -1
- package/src/appearance/NineSliceImageFrame.tsx +1 -1
- package/src/appearance/ThemeSurface.tsx +1 -1
- package/src/batch-tasks/BatchProjectFoldersFieldView.tsx +2 -1
- package/src/batch-tasks/BatchTaskProjectActionsView.test.tsx +97 -0
- package/src/batch-tasks/BatchTaskProjectActionsView.tsx +45 -35
- package/src/batch-tasks/batchTaskUi.tsx +0 -29
- package/src/bottom-panel/BottomPanelView.test.tsx +43 -0
- package/src/bottom-panel/BottomPanelView.tsx +222 -0
- package/src/bottom-panel/index.ts +26 -0
- package/src/chat/AgentAvatarView.tsx +1 -1
- package/src/chat/AnnotationViews.tsx +107 -0
- package/src/chat/AskUserQuestionView.tsx +18 -11
- package/src/chat/AssistantMessageView.tsx +48 -13
- package/src/chat/AtPanelView.test.tsx +61 -0
- package/src/chat/AtPanelView.tsx +63 -38
- package/src/chat/BashTerminalCard.tsx +1 -2
- package/src/chat/ChatHeaderActionsView.tsx +74 -18
- package/src/chat/CompactionStepView.test.tsx +34 -0
- package/src/chat/CompactionStepView.tsx +85 -0
- package/src/chat/ExecutionModeSelectorView.tsx +20 -5
- package/src/chat/HeaderActionBarView.tsx +731 -0
- package/src/chat/HeaderActionOverflowMenu.tsx +137 -0
- package/src/chat/InlineModelPicker.test.tsx +81 -0
- package/src/chat/InlineModelPicker.tsx +201 -0
- package/src/chat/InlineTokenChip.tsx +2 -68
- package/src/chat/InputBarBackground.tsx +1 -1
- package/src/chat/InputBarPlaceholder.tsx +26 -3
- package/src/chat/LiveThinkingView.tsx +51 -37
- package/src/chat/MessageBoundaryViews.tsx +21 -0
- package/src/chat/MessageFeedContext.tsx +13 -5
- package/src/chat/MessageFeedLayoutView.tsx +28 -3
- package/src/chat/MessageFeedView.test.tsx +81 -0
- package/src/chat/MessageFeedView.tsx +72 -152
- package/src/chat/MessageInputView.tsx +1 -1
- package/src/chat/MessageLayoutView.tsx +1 -1
- package/src/chat/MessageSelectionContextMenuView.tsx +4 -1
- package/src/chat/MessageTimelineView.tsx +1 -1
- package/src/chat/MessageView.tsx +1 -1
- package/src/chat/MessageVisualView.tsx +1 -1
- package/src/chat/ModelConfiguration.tsx +283 -0
- package/src/chat/ModelSelectorTrigger.tsx +63 -0
- package/src/chat/ModelSelectorView.tsx +1037 -223
- package/src/chat/NewSession.ts +1 -37
- package/src/chat/NewSessionPicker.tsx +3 -2
- package/src/chat/ProjectSelectorView.tsx +3 -2
- package/src/chat/ReasoningStepSlider.tsx +61 -0
- package/src/chat/RemoteSessionBadgeView.tsx +24 -0
- package/src/chat/SkillPromptAreaView.tsx +5 -19
- package/src/chat/TeamMemberReplyCardView.test.tsx +33 -0
- package/src/chat/TeamMemberReplyCardView.tsx +133 -0
- package/src/chat/TextBlockView.tsx +7 -770
- package/src/chat/TodoProgress.tsx +25 -53
- package/src/chat/TodoStatusBarView.tsx +14 -5
- package/src/chat/UserMessageView.tsx +113 -33
- package/src/chat/header-action-narrow.test.ts +21 -0
- package/src/chat/header-action-narrow.ts +38 -0
- package/src/chat/index.ts +32 -27
- package/src/chat/markdown-link.ts +7 -279
- package/src/chat/send-button.css +4 -22
- package/src/chat/streaming-reveal.test.ts +218 -0
- package/src/chat/streaming-reveal.ts +13 -0
- package/src/file-explorer/ConfirmDeleteDialogView.tsx +2 -1
- package/src/file-explorer/FileContextMenuView.tsx +44 -11
- package/src/file-explorer/FileExplorerSettingsView.tsx +97 -0
- package/src/file-explorer/FileTreeCreateRow.tsx +1 -1
- package/src/file-explorer/FileTreeNodeView.tsx +59 -18
- package/src/file-explorer/FileTreeView.tsx +236 -129
- package/src/file-explorer/file-tree-dom.ts +23 -0
- package/src/file-explorer/file-tree-rows.test.ts +74 -0
- package/src/file-explorer/file-tree-rows.ts +168 -0
- package/src/file-explorer/index.ts +17 -0
- package/src/file-explorer/types.ts +3 -0
- package/src/file-explorer/useFileTreeMarqueeSelection.ts +24 -16
- package/src/file-preview/FilePreviewDialogView.tsx +2 -2
- package/src/file-preview/FilePreviewView.tsx +12 -2
- package/src/file-preview/TextFileEditorView.tsx +2 -1
- package/src/index.ts +1 -0
- package/src/knowledge/KnowledgeHowItWorksDialogView.tsx +1 -1
- package/src/layout/AppFrame.tsx +3 -2
- package/src/layout/MainContentFrame.tsx +7 -2
- package/src/layout/ResizeHandle.tsx +68 -17
- package/src/layout/SidebarDock.tsx +75 -19
- package/src/layout/SidebarOverlay.tsx +1 -1
- package/src/layout/index.ts +1 -1
- package/src/layout/resize-handle-axis.test.ts +51 -0
- package/src/live-indicators.test.ts +43 -0
- package/src/markdown/CodeBlock.tsx +123 -0
- package/src/markdown/InlineTokenChip.tsx +68 -0
- package/src/markdown/MarkdownContent.tsx +411 -0
- package/src/markdown/MarkdownImage.tsx +51 -0
- package/src/markdown/MathFormula.tsx +46 -0
- package/src/markdown/MermaidBlock.tsx +43 -0
- package/src/markdown/RichCodeBlock.tsx +127 -0
- package/src/markdown/SvgPreview.tsx +45 -0
- package/src/markdown/WebsiteIcon.tsx +94 -0
- package/src/markdown/builtin-renderers.tsx +43 -0
- package/src/markdown/definition.tsx +49 -0
- package/src/markdown/host.tsx +18 -0
- package/src/markdown/index.ts +11 -0
- package/src/markdown/inline-tokens.ts +187 -0
- package/src/markdown/markdown-link.ts +279 -0
- package/src/markdown/math-client.ts +116 -0
- package/src/markdown/math-render.ts +23 -0
- package/src/markdown/math-syntax.ts +80 -0
- package/src/markdown/math.worker.ts +16 -0
- package/src/markdown/nodes.ts +15 -0
- package/src/markdown/preview-policy.ts +43 -0
- package/src/markdown/rich-labels.ts +49 -0
- package/src/markdown/rich-syntax.ts +93 -0
- package/src/markdown/stable-blocks.test.ts +90 -0
- package/src/markdown/stable-blocks.ts +124 -0
- package/src/markdown/streaming-reveal.ts +297 -0
- package/src/markdown/streaming.test.ts +57 -0
- package/src/markdown/streaming.ts +211 -0
- package/src/markdown/use-render-snapshot.ts +24 -0
- package/src/markdown/use-rich-visibility.ts +24 -0
- package/src/overlays/CommandMenuRow.tsx +38 -9
- package/src/overlays/CommandMenuView.tsx +77 -117
- package/src/overlays/ConfirmDialogView.tsx +25 -20
- package/src/overlays/ReleaseNoteMarkdown.tsx +29 -0
- package/src/overlays/SplashOptionRow.tsx +46 -0
- package/src/overlays/SplashPanel.tsx +85 -0
- package/src/overlays/SplashParts.tsx +213 -0
- package/src/overlays/UpdateCheckerView.tsx +34 -18
- package/src/overlays/UpdateRestartDialogView.tsx +139 -54
- package/src/overlays/command-menu-types.ts +2 -0
- package/src/overlays/command-menu.css +1 -20
- package/src/overlays/index.ts +14 -1
- package/src/plugin-ui/index.ts +8 -0
- package/src/project/ConversationTagDotsView.tsx +1 -1
- package/src/project/ConversationTagEditorDialogView.tsx +4 -1
- package/src/project/DefaultConversationSectionView.tsx +4 -4
- package/src/project/DefaultSessionListView.tsx +5 -31
- package/src/project/DefaultSessionRowView.tsx +106 -55
- package/src/project/ManageSessionsSplashView.tsx +439 -0
- package/src/project/NewProjectDialogView.tsx +3 -10
- package/src/project/ProjectContextMenuView.tsx +30 -139
- package/src/project/ProjectDetailPageView.test.tsx +140 -0
- package/src/project/ProjectDetailPageView.tsx +372 -165
- package/src/project/ProjectGroupView.tsx +1 -1
- package/src/project/ProjectRowView.tsx +29 -12
- package/src/project/ProjectSessionsView.tsx +25 -47
- package/src/project/QuickScrollOverlay.tsx +1 -1
- package/src/project/SessionContextMenuView.tsx +15 -7
- package/src/project/SessionRowMoreButton.tsx +50 -0
- package/src/project/SessionRowView.tsx +106 -63
- package/src/project/SessionTitleMarquee.tsx +54 -0
- package/src/project/SidebarProjectDisclosure.tsx +60 -0
- package/src/project/SidebarProjectsSectionView.tsx +6 -1
- package/src/project/index.ts +12 -0
- package/src/project/session-row-transition.ts +9 -0
- package/src/project/types.ts +4 -2
- package/src/project/useActiveSessionAutoScroll.ts +10 -4
- package/src/registry.ts +0 -2
- package/src/scheduler/AutomationDetailPaneView.tsx +68 -0
- package/src/scheduler/AutomationPageView.tsx +118 -121
- package/src/scheduler/ExecutionHistoryView.tsx +7 -5
- package/src/scheduler/TaskListView.tsx +52 -201
- package/src/scheduler/index.ts +7 -12
- package/src/settings/AchievementSettingsView.tsx +0 -1
- package/src/settings/AppshotSettingsView.tsx +0 -1
- package/src/settings/BuiltinMcpSectionView.tsx +1 -1
- package/src/settings/EnvironmentGitRowView.tsx +156 -0
- package/src/settings/EnvironmentSettingsView.tsx +25 -3
- package/src/settings/GeneralSettingsView.tsx +17 -20
- package/src/settings/ImChannelGuideDialogView.tsx +2 -2
- package/src/settings/ImFeishuDialogView.tsx +1 -1
- package/src/settings/ImLegacyImportBannerView.tsx +1 -1
- package/src/settings/ImLogDrawerView.tsx +2 -1
- package/src/settings/JsonConfigInputView.tsx +171 -0
- package/src/settings/ModelsFetchedModelsDrawerView.tsx +292 -0
- package/src/settings/ModelsProvidersSectionView.tsx +26 -23
- package/src/settings/MotionSelect.tsx +9 -7
- package/src/settings/NotificationSettingsView.tsx +79 -0
- package/src/settings/PermissionsSettingsView.tsx +3 -7
- package/src/settings/PresetProviderModelsListView.tsx +141 -24
- package/src/settings/PresetProviderRowView.tsx +205 -182
- package/src/settings/ProxySettingsSectionView.tsx +182 -0
- package/src/settings/QrBindDialogView.tsx +1 -1
- package/src/settings/QuickPanelSettingsSectionView.tsx +0 -2
- package/src/settings/RemoteMcpSectionView.tsx +1 -1
- package/src/settings/SettingChrome.tsx +2 -2
- package/src/settings/SettingsAiAssistButtonView.tsx +9 -119
- package/src/settings/SettingsFormFields.tsx +4 -7
- package/src/settings/SettingsSidebarView.tsx +64 -3
- package/src/settings/ShortcutRecorderView.test.tsx +108 -0
- package/src/settings/ShortcutRecorderView.tsx +15 -9
- package/src/settings/ShortcutsSettingsView.tsx +1 -1
- package/src/settings/SpaceEditorDialogView.tsx +120 -0
- package/src/settings/SpacesSettingsView.tsx +105 -0
- package/src/settings/WebhookEndpointListView.tsx +4 -5
- package/src/settings/index.ts +41 -29
- package/src/shared/ActivityStatusDot.tsx +69 -0
- package/src/shared/AvatarStackView.tsx +13 -13
- package/src/shared/BotAvatar.tsx +11 -9
- package/src/shared/CodeBlockCopyButton.tsx +1 -1
- package/src/shared/ContextMenuView.tsx +4 -11
- package/src/shared/MarkdownTable.tsx +2 -2
- package/src/shared/SyntaxHighlightedCode.tsx +0 -0
- package/src/shared/TabBar.outline.test.tsx +59 -0
- package/src/shared/TabBar.tsx +22 -17
- package/src/shared/VettaKbd.test.tsx +52 -0
- package/src/shared/VettaKbd.tsx +81 -0
- package/src/shared/connected-tab-outline.test.ts +2 -2
- package/src/shared/connected-tab-outline.ts +2 -1
- package/src/shared/index.ts +6 -0
- package/src/shared/kbd-keys.test.ts +61 -0
- package/src/shared/kbd-keys.ts +253 -0
- package/src/shared/provider-icon/icons.ts +4 -2
- package/src/shared/provider-icon/index.tsx +1 -1
- package/src/shared/provider-icon/magpie.svg +1 -0
- package/src/sidebar/AddProjectMenuTriggerView.tsx +25 -15
- package/src/sidebar/DefaultSidebar.tsx +3 -1
- package/src/sidebar/MessageCenterDialogView.tsx +3 -2
- package/src/sidebar/MessageCenterEmptyState.tsx +1 -1
- package/src/sidebar/MessageCenterTabs.tsx +1 -1
- package/src/sidebar/MessageCenterToolbarButton.tsx +1 -1
- package/src/sidebar/MessageCenterTriggerView.tsx +2 -2
- package/src/sidebar/ProjectsPanelEmptyState.tsx +26 -7
- package/src/sidebar/SessionStatusIcon.tsx +16 -1
- package/src/sidebar/SettingsMenuThemeSection.tsx +1 -1
- package/src/sidebar/SettingsMenuTriggerView.test.tsx +42 -0
- package/src/sidebar/SettingsMenuTriggerView.tsx +28 -50
- package/src/sidebar/ShowMoreSessionsButton.tsx +1 -1
- package/src/sidebar/SidebarFilterSelectView.tsx +8 -16
- package/src/sidebar/SidebarNavBadgeView.tsx +1 -1
- package/src/sidebar/SidebarNavIcon.tsx +1 -1
- package/src/sidebar/SidebarNavItemButton.tsx +1 -1
- package/src/sidebar/SidebarNavMorePanel.tsx +19 -7
- package/src/sidebar/SidebarNavigation.tsx +24 -15
- package/src/sidebar/SidebarPanel.tsx +11 -2
- package/src/sidebar/SidebarSectionTriggerLabel.tsx +20 -0
- package/src/sidebar/SidebarTopBar.tsx +10 -7
- package/src/sidebar/WaitingPulseBadge.tsx +19 -0
- package/src/sidebar/index.ts +1 -16
- package/src/skills/index.ts +0 -19
- package/src/skills/skill-icon.tsx +4 -8
- package/src/styles.css +24 -6
- package/src/chat/DefaultSceneCarousel.tsx +0 -113
- package/src/chat/SceneCard.tsx +0 -138
- package/src/scheduler/HistoryDrawerView.tsx +0 -149
- package/src/scheduler/TaskFormDialogView.tsx +0 -60
- package/src/settings/SubscriptionActionsView.tsx +0 -63
- package/src/settings/SubscriptionCardsView.tsx +0 -212
- package/src/settings/TeamDetailView.tsx +0 -148
- package/src/settings/TeamDialogsView.tsx +0 -151
- package/src/settings/TeamListView.tsx +0 -57
- package/src/settings/TeamSettingsView.tsx +0 -51
- package/src/settings/TokenActivityChartView.tsx +0 -286
- package/src/settings/UsageStatsView.tsx +0 -101
- package/src/settings/shouldCloseEmptyApiKeyEditor.test.ts +0 -135
- package/src/settings/shouldCloseEmptyApiKeyEditor.ts +0 -40
- package/src/settings/token-activity.test.ts +0 -195
- package/src/settings/token-activity.ts +0 -320
- package/src/sidebar/AddProjectMenuItem.tsx +0 -21
- package/src/sidebar/AddProjectMenuPopoverView.tsx +0 -52
- package/src/sidebar/NotificationMessageListView.tsx +0 -136
- package/src/sidebar/SettingsMenuAccountSection.tsx +0 -32
- package/src/sidebar/SettingsMenuQuotaSection.tsx +0 -45
- package/src/skills/SceneCardView.tsx +0 -194
- package/src/skills/SkillCardView.tsx +0 -186
- package/src/skills/SkillDetailDialogView.tsx +0 -138
- package/src/skills/SkillTagGroupView.tsx +0 -56
- package/src/skills/SkillToggleSwitch.tsx +0 -31
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classify markdown `href` values for chat text blocks.
|
|
3
|
+
*
|
|
4
|
+
* LLMs emit many shapes: `file://`, POSIX `/abs`, Windows `C:\…`,
|
|
5
|
+
* relative `./foo` / `src/a.ts`, and real `https://` URLs. Misclassifying a
|
|
6
|
+
* local path as a generic anchor (especially with `target="_blank"`) opens
|
|
7
|
+
* the system browser instead of the in-app file preview.
|
|
8
|
+
*
|
|
9
|
+
* Also: `react-markdown`'s `defaultUrlTransform` treats `C:` as an unknown
|
|
10
|
+
* protocol and blanks the href — Windows paths must be allowlisted via
|
|
11
|
+
* {@link chatUrlTransform}. Models also occasionally omit CommonMark's angle
|
|
12
|
+
* brackets around local destinations that contain whitespace. The normalizer
|
|
13
|
+
* repairs those legacy forms before parse and standardizes local destinations.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { defaultUrlTransform } from "react-markdown";
|
|
17
|
+
|
|
18
|
+
export type MarkdownLinkKind =
|
|
19
|
+
| { type: "file"; path: string }
|
|
20
|
+
| { type: "url"; url: string }
|
|
21
|
+
| { type: "other"; href: string };
|
|
22
|
+
|
|
23
|
+
function safeDecode(raw: string): string {
|
|
24
|
+
try {
|
|
25
|
+
return decodeURIComponent(raw);
|
|
26
|
+
} catch {
|
|
27
|
+
return raw;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function toPosixSeparators(path: string): string {
|
|
32
|
+
return path.replace(/\\/g, "/");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Allow local filesystem hrefs that `defaultUrlTransform` would strip because
|
|
37
|
+
* it sees `C:` / `file:` as unsafe protocols. Everything else keeps the
|
|
38
|
+
* library default (blocks `javascript:` etc.).
|
|
39
|
+
*/
|
|
40
|
+
export function chatUrlTransform(url: string): string {
|
|
41
|
+
// Windows drive: C:\… C:/… or rehype-encoded C:%5C…
|
|
42
|
+
if (/^[A-Za-z]:[\\/]/.test(url) || /^[A-Za-z]:%5[Cc]/i.test(url)) {
|
|
43
|
+
return url;
|
|
44
|
+
}
|
|
45
|
+
// file:// local URLs
|
|
46
|
+
if (/^file:/i.test(url)) {
|
|
47
|
+
return url;
|
|
48
|
+
}
|
|
49
|
+
return defaultUrlTransform(url);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
interface MarkdownFence {
|
|
53
|
+
character: "`" | "~";
|
|
54
|
+
length: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const STANDARD_LINK_TITLE = /^(\S+)(\s+(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\([^()]*\)))$/;
|
|
58
|
+
|
|
59
|
+
function splitStandardLinkTitle(destination: string): { path: string; title: string } {
|
|
60
|
+
const match = STANDARD_LINK_TITLE.exec(destination);
|
|
61
|
+
return match ? { path: match[1], title: match[2] } : { path: destination, title: "" };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function normalizeAbsoluteLocalDestination(path: string): string | null {
|
|
65
|
+
if (!path || /[<>\r\n]/.test(path)) return null;
|
|
66
|
+
if (/^\\\\[^\\]/.test(path)) {
|
|
67
|
+
return `file://${path.slice(2).replace(/\\/g, "/")}`;
|
|
68
|
+
}
|
|
69
|
+
if (/^file:/i.test(path)) return path.replace(/\\/g, "/");
|
|
70
|
+
if (/^\/?[A-Za-z]:[\\/]/.test(path)) return path.replace(/\\/g, "/");
|
|
71
|
+
if (path.startsWith("/") && !path.startsWith("//")) return path;
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function normalizeBareDestination(destination: string): string | null {
|
|
76
|
+
const { path, title } = splitStandardLinkTitle(destination);
|
|
77
|
+
const normalized = normalizeAbsoluteLocalDestination(path);
|
|
78
|
+
return normalized ? `<${normalized}>${title}` : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function findBareDestinationEnd(line: string, start: number): number {
|
|
82
|
+
let parenthesesDepth = 0;
|
|
83
|
+
for (let index = start; index < line.length; index++) {
|
|
84
|
+
if (line[index] === "(") {
|
|
85
|
+
parenthesesDepth++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (line[index] !== ")") continue;
|
|
89
|
+
if (parenthesesDepth === 0) return index;
|
|
90
|
+
parenthesesDepth--;
|
|
91
|
+
}
|
|
92
|
+
return -1;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function normalizeMarkdownLine(line: string): string {
|
|
96
|
+
if (/^(?: {4}|\t)/.test(line)) return line;
|
|
97
|
+
|
|
98
|
+
let result = "";
|
|
99
|
+
let cursor = 0;
|
|
100
|
+
let codeDelimiterLength = 0;
|
|
101
|
+
for (let index = 0; index < line.length; ) {
|
|
102
|
+
if (line[index] === "`") {
|
|
103
|
+
let end = index + 1;
|
|
104
|
+
while (line[end] === "`") end++;
|
|
105
|
+
const runLength = end - index;
|
|
106
|
+
if (codeDelimiterLength === 0) codeDelimiterLength = runLength;
|
|
107
|
+
else if (runLength === codeDelimiterLength) codeDelimiterLength = 0;
|
|
108
|
+
index = end;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (codeDelimiterLength !== 0 || line[index] !== "]" || line[index + 1] !== "(") {
|
|
113
|
+
index++;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const destinationStart = index + 2;
|
|
118
|
+
if (line[destinationStart] === "<") {
|
|
119
|
+
const angleEnd = line.indexOf(">)", destinationStart + 1);
|
|
120
|
+
if (angleEnd === -1) {
|
|
121
|
+
index++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const normalized = normalizeAbsoluteLocalDestination(line.slice(destinationStart + 1, angleEnd));
|
|
125
|
+
if (!normalized) {
|
|
126
|
+
index = angleEnd + 2;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
result += `${line.slice(cursor, destinationStart)}<${normalized}>)`;
|
|
130
|
+
cursor = angleEnd + 2;
|
|
131
|
+
index = cursor;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const destinationEnd = findBareDestinationEnd(line, destinationStart);
|
|
136
|
+
if (destinationEnd === -1) {
|
|
137
|
+
index++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
const normalized = normalizeBareDestination(line.slice(destinationStart, destinationEnd));
|
|
141
|
+
if (!normalized) {
|
|
142
|
+
index = destinationEnd + 1;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
result += `${line.slice(cursor, destinationStart)}${normalized})`;
|
|
146
|
+
cursor = destinationEnd + 1;
|
|
147
|
+
index = cursor;
|
|
148
|
+
}
|
|
149
|
+
return result + line.slice(cursor);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Standardize absolute local link destinations as CommonMark angle-bracket
|
|
154
|
+
* destinations before parsing. This makes whitespace and balanced parentheses
|
|
155
|
+
* unambiguous, converts Windows separators, and repairs already persisted model
|
|
156
|
+
* output such as `[file](/C:/Users/name/My Files/file.md)`.
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
159
|
+
export function normalizeLocalFileLinksInMarkdown(markdown: string): string {
|
|
160
|
+
const parts = markdown.split(/(\r\n|\n|\r)/);
|
|
161
|
+
let fence: MarkdownFence | null = null;
|
|
162
|
+
return parts
|
|
163
|
+
.map((part) => {
|
|
164
|
+
if (part === "\n" || part === "\r" || part === "\r\n") return part;
|
|
165
|
+
const marker = /^ {0,3}(`{3,}|~{3,})/.exec(part)?.[1];
|
|
166
|
+
if (marker) {
|
|
167
|
+
const character = marker[0] as MarkdownFence["character"];
|
|
168
|
+
if (!fence) fence = { character, length: marker.length };
|
|
169
|
+
else if (
|
|
170
|
+
character === fence.character &&
|
|
171
|
+
marker.length >= fence.length &&
|
|
172
|
+
part.slice(part.indexOf(marker) + marker.length).trim() === ""
|
|
173
|
+
) {
|
|
174
|
+
fence = null;
|
|
175
|
+
}
|
|
176
|
+
return part;
|
|
177
|
+
}
|
|
178
|
+
return fence ? part : normalizeMarkdownLine(part);
|
|
179
|
+
})
|
|
180
|
+
.join("");
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Convert a `file://` URL to an OS path (POSIX absolute or Windows drive/UNC).
|
|
185
|
+
* Returns null when the input is not a usable file URL.
|
|
186
|
+
*/
|
|
187
|
+
export function fileUrlToPath(href: string): string | null {
|
|
188
|
+
try {
|
|
189
|
+
const url = new URL(href);
|
|
190
|
+
if (url.protocol !== "file:") return null;
|
|
191
|
+
let path = safeDecode(url.pathname);
|
|
192
|
+
// Windows drive: /C:/Users → C:/Users
|
|
193
|
+
if (/^\/[A-Za-z]:/.test(path)) {
|
|
194
|
+
path = path.slice(1);
|
|
195
|
+
}
|
|
196
|
+
// UNC: file://server/share/path → //server/share/path
|
|
197
|
+
if (url.hostname && url.hostname !== "localhost") {
|
|
198
|
+
const body = path.startsWith("/") ? path : `/${path}`;
|
|
199
|
+
path = `//${url.hostname}${body}`;
|
|
200
|
+
}
|
|
201
|
+
return toPosixSeparators(path) || null;
|
|
202
|
+
} catch {
|
|
203
|
+
const stripped = safeDecode(href.replace(/^file:\/\//i, ""));
|
|
204
|
+
if (!stripped) return null;
|
|
205
|
+
if (/^\/[A-Za-z]:/.test(stripped)) {
|
|
206
|
+
return toPosixSeparators(stripped.slice(1));
|
|
207
|
+
}
|
|
208
|
+
return toPosixSeparators(stripped);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Bare names like `readme.md` should open as files; bare domains like
|
|
214
|
+
* `www.example.com` must not. Prefer path separators / relative prefixes;
|
|
215
|
+
* fall back to a conservative extension allowlist (not generic TLDs).
|
|
216
|
+
*/
|
|
217
|
+
const LIKELY_FILE_EXT =
|
|
218
|
+
/\.(?:tsx?|jsx?|mjs|cjs|json|mdx?|txt|pdf|png|jpe?g|gif|webp|svg|html?|css|scss|less|py|rs|go|java|kt|swift|c|cc|cpp|h|hpp|cs|rb|php|sh|bash|zsh|ya?ml|toml|xml|csv|tsv|xlsx?|docx?|pptx?|zip|gz|tgz|7z|rar|mp3|mp4|wav|webm|ico|woff2?|ttf|otf|map|sql|vue|svelte|astro|wasm|log|conf|cfg|ini|plist|ipynb|rtf|lock|env)$/i;
|
|
219
|
+
|
|
220
|
+
function looksLikeRelativeFilePath(value: string): boolean {
|
|
221
|
+
if (/^[.]{1,2}[\\/]/.test(value)) return true;
|
|
222
|
+
if (/[\\/]/.test(value)) return true;
|
|
223
|
+
return LIKELY_FILE_EXT.test(value);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Classify a markdown link href into file / external URL / other.
|
|
228
|
+
* File paths are returned with `/` separators; relative paths are left relative
|
|
229
|
+
* for the host to resolve against the project cwd.
|
|
230
|
+
*/
|
|
231
|
+
export function classifyMarkdownLink(href: string | undefined): MarkdownLinkKind {
|
|
232
|
+
if (!href) return { type: "other", href: "" };
|
|
233
|
+
const trimmed = href.trim();
|
|
234
|
+
if (!trimmed || trimmed.startsWith("#")) {
|
|
235
|
+
return { type: "other", href: trimmed };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (/^(mailto|tel|javascript|data|blob|vetta):/i.test(trimmed)) {
|
|
239
|
+
return { type: "other", href: trimmed };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (/^https?:\/\//i.test(trimmed)) {
|
|
243
|
+
return { type: "url", url: trimmed };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Protocol-relative URL (//cdn.example.com/x) — not a Windows UNC path.
|
|
247
|
+
if (trimmed.startsWith("//") && !trimmed.startsWith("\\\\")) {
|
|
248
|
+
return { type: "url", url: `https:${trimmed}` };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (/^file:\/\//i.test(trimmed)) {
|
|
252
|
+
const path = fileUrlToPath(trimmed);
|
|
253
|
+
if (path) return { type: "file", path };
|
|
254
|
+
return { type: "other", href: trimmed };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const decoded = safeDecode(trimmed);
|
|
258
|
+
|
|
259
|
+
// Windows drive absolute: C:\… or C:/… (after decode of %5C)
|
|
260
|
+
if (/^[A-Za-z]:[\\/]/.test(decoded)) {
|
|
261
|
+
return { type: "file", path: toPosixSeparators(decoded) };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Windows UNC: \\server\share
|
|
265
|
+
if (decoded.startsWith("\\\\")) {
|
|
266
|
+
return { type: "file", path: toPosixSeparators(decoded) };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// POSIX absolute
|
|
270
|
+
if (decoded.startsWith("/")) {
|
|
271
|
+
return { type: "file", path: decoded };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (looksLikeRelativeFilePath(decoded)) {
|
|
275
|
+
return { type: "file", path: toPosixSeparators(decoded) };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return { type: "other", href: trimmed };
|
|
279
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
type Listener = (html: string | null) => void;
|
|
2
|
+
interface Job {
|
|
3
|
+
id: number;
|
|
4
|
+
key: string;
|
|
5
|
+
source: string;
|
|
6
|
+
display: boolean;
|
|
7
|
+
listeners: Set<Listener>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const cache = new Map<string, string | null>();
|
|
11
|
+
let cacheSize = 0;
|
|
12
|
+
const jobs = new Map<string, Job>();
|
|
13
|
+
let worker: Worker | null = null;
|
|
14
|
+
let running: Job | null = null;
|
|
15
|
+
let sequence = 0;
|
|
16
|
+
let deadline: ReturnType<typeof setTimeout> | undefined;
|
|
17
|
+
let idle: ReturnType<typeof setTimeout> | undefined;
|
|
18
|
+
|
|
19
|
+
function disposeWorker() {
|
|
20
|
+
clearTimeout(deadline);
|
|
21
|
+
clearTimeout(idle);
|
|
22
|
+
worker?.terminate();
|
|
23
|
+
worker = null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function finish(html: string | null) {
|
|
27
|
+
clearTimeout(deadline);
|
|
28
|
+
const job = running;
|
|
29
|
+
if (!job) return;
|
|
30
|
+
running = null;
|
|
31
|
+
jobs.delete(job.key);
|
|
32
|
+
const size = job.key.length + (html?.length ?? 0);
|
|
33
|
+
if (size <= 1_000_000) {
|
|
34
|
+
while (cache.size >= 256 || cacheSize + size > 1_000_000) {
|
|
35
|
+
const oldest = cache.keys().next().value;
|
|
36
|
+
if (oldest === undefined) break;
|
|
37
|
+
cacheSize -= oldest.length + (cache.get(oldest)?.length ?? 0);
|
|
38
|
+
cache.delete(oldest);
|
|
39
|
+
}
|
|
40
|
+
cache.set(job.key, html);
|
|
41
|
+
cacheSize += size;
|
|
42
|
+
}
|
|
43
|
+
for (const listener of job.listeners) listener(html);
|
|
44
|
+
pump();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function pump() {
|
|
48
|
+
if (running) return;
|
|
49
|
+
const job = jobs.values().next().value;
|
|
50
|
+
if (!job) {
|
|
51
|
+
idle = setTimeout(disposeWorker, 30_000);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
clearTimeout(idle);
|
|
55
|
+
running = job;
|
|
56
|
+
try {
|
|
57
|
+
if (!worker) {
|
|
58
|
+
const instance = new Worker(new URL("./math.worker.ts", import.meta.url), { type: "module" });
|
|
59
|
+
worker = instance;
|
|
60
|
+
instance.onmessage = (event: MessageEvent<unknown>) => {
|
|
61
|
+
if (worker !== instance) return;
|
|
62
|
+
const data = event.data;
|
|
63
|
+
if (!data || typeof data !== "object" || !("id" in data) || !("html" in data)) return;
|
|
64
|
+
if (data.id !== running?.id || (data.html !== null && typeof data.html !== "string")) return;
|
|
65
|
+
finish(data.html);
|
|
66
|
+
};
|
|
67
|
+
instance.onerror = () => {
|
|
68
|
+
if (worker !== instance) return;
|
|
69
|
+
disposeWorker();
|
|
70
|
+
finish(null);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
deadline = setTimeout(() => {
|
|
74
|
+
disposeWorker();
|
|
75
|
+
finish(null);
|
|
76
|
+
}, 2000);
|
|
77
|
+
worker.postMessage({ id: job.id, source: job.source, display: job.display });
|
|
78
|
+
} catch {
|
|
79
|
+
disposeWorker();
|
|
80
|
+
finish(null);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Deduplicated, serial and cancellable; detached formulas never keep queued work alive. */
|
|
85
|
+
export function requestFormula(source: string, display: boolean, listener: Listener): () => void {
|
|
86
|
+
if (source.length > 8192) {
|
|
87
|
+
listener(null);
|
|
88
|
+
return () => {};
|
|
89
|
+
}
|
|
90
|
+
const key = JSON.stringify([source, display]);
|
|
91
|
+
if (cache.has(key)) {
|
|
92
|
+
listener(cache.get(key) ?? null);
|
|
93
|
+
return () => {};
|
|
94
|
+
}
|
|
95
|
+
let job = jobs.get(key);
|
|
96
|
+
if (!job) {
|
|
97
|
+
if (jobs.size >= 128) {
|
|
98
|
+
listener(null);
|
|
99
|
+
return () => {};
|
|
100
|
+
}
|
|
101
|
+
job = { id: ++sequence, key, source, display, listeners: new Set() };
|
|
102
|
+
jobs.set(key, job);
|
|
103
|
+
}
|
|
104
|
+
job.listeners.add(listener);
|
|
105
|
+
pump();
|
|
106
|
+
return () => {
|
|
107
|
+
job.listeners.delete(listener);
|
|
108
|
+
if (job.listeners.size !== 0) return;
|
|
109
|
+
jobs.delete(key);
|
|
110
|
+
if (running === job) {
|
|
111
|
+
disposeWorker();
|
|
112
|
+
running = null;
|
|
113
|
+
pump();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import katex from "katex";
|
|
2
|
+
|
|
3
|
+
export const MAX_FORMULA_LENGTH = 8192;
|
|
4
|
+
|
|
5
|
+
/** Called only in the worker; each formula gets its own macro scope. */
|
|
6
|
+
export function renderFormula(source: string, displayMode: boolean): string | null {
|
|
7
|
+
if (source.length > MAX_FORMULA_LENGTH) return null;
|
|
8
|
+
try {
|
|
9
|
+
const html = katex.renderToString(source, {
|
|
10
|
+
displayMode,
|
|
11
|
+
output: "htmlAndMathml",
|
|
12
|
+
trust: false,
|
|
13
|
+
throwOnError: true,
|
|
14
|
+
strict: "ignore",
|
|
15
|
+
maxExpand: 500,
|
|
16
|
+
maxSize: 20,
|
|
17
|
+
macros: {},
|
|
18
|
+
});
|
|
19
|
+
return html.length <= 1_000_000 ? html : null;
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Extension } from "mdast-util-from-markdown";
|
|
2
|
+
import type { Tokenizer } from "micromark-util-types";
|
|
3
|
+
import type { Processor } from "unified";
|
|
4
|
+
|
|
5
|
+
declare module "micromark-util-types" {
|
|
6
|
+
interface TokenTypeMap {
|
|
7
|
+
vettaMath: "vettaMath";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const tokenize: Tokenizer = (effects, ok, nok) => {
|
|
12
|
+
let closing = 41;
|
|
13
|
+
let count = 0;
|
|
14
|
+
return start;
|
|
15
|
+
function start(code: number | null) {
|
|
16
|
+
effects.enter("vettaMath");
|
|
17
|
+
effects.consume(code);
|
|
18
|
+
return delimiter;
|
|
19
|
+
}
|
|
20
|
+
function delimiter(code: number | null) {
|
|
21
|
+
if (code !== 40 && code !== 91) return nok(code);
|
|
22
|
+
closing = code === 40 ? 41 : 93;
|
|
23
|
+
effects.consume(code);
|
|
24
|
+
return body;
|
|
25
|
+
}
|
|
26
|
+
function body(code: number | null): ReturnType<Tokenizer> | undefined {
|
|
27
|
+
if (code === null || ++count > 16_000) return nok(code);
|
|
28
|
+
effects.consume(code);
|
|
29
|
+
return code === 92 ? end : body;
|
|
30
|
+
}
|
|
31
|
+
function end(code: number | null): ReturnType<Tokenizer> | undefined {
|
|
32
|
+
if (code === closing) {
|
|
33
|
+
effects.consume(code);
|
|
34
|
+
effects.exit("vettaMath");
|
|
35
|
+
return ok;
|
|
36
|
+
}
|
|
37
|
+
if (code === 40 || code === 91) return nok(code);
|
|
38
|
+
// A doubled slash is TeX's line break, not a closing delimiter.
|
|
39
|
+
if (code === 92) {
|
|
40
|
+
effects.consume(code);
|
|
41
|
+
return body;
|
|
42
|
+
}
|
|
43
|
+
return body(code);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Tokenize before Markdown escaping; fenced/inline code and link destinations remain untouched. */
|
|
48
|
+
export function remarkMathAliases(this: Processor) {
|
|
49
|
+
const data = this.data();
|
|
50
|
+
data.micromarkExtensions ??= [];
|
|
51
|
+
data.micromarkExtensions.push({ text: { 92: { name: "vettaMath", tokenize } } });
|
|
52
|
+
const extension: Extension = {
|
|
53
|
+
enter: {
|
|
54
|
+
vettaMath(token) {
|
|
55
|
+
const raw = this.sliceSerialize(token);
|
|
56
|
+
this.enter(
|
|
57
|
+
{
|
|
58
|
+
type: "inlineMath",
|
|
59
|
+
value: raw.slice(2, -2),
|
|
60
|
+
data: {
|
|
61
|
+
hName: "code",
|
|
62
|
+
hProperties: { className: ["language-math", raw[1] === "[" ? "math-display" : "math-inline"] },
|
|
63
|
+
hChildren: [{ type: "text", value: raw.slice(2, -2) }],
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
token,
|
|
67
|
+
);
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
exit: {
|
|
71
|
+
vettaMath(token) {
|
|
72
|
+
this.exit(token);
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
data.fromMarkdownExtensions ??= [];
|
|
77
|
+
data.fromMarkdownExtensions.push(extension);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
import type {} from "remark-parse";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { renderFormula } from "./math-render";
|
|
2
|
+
|
|
3
|
+
globalThis.onmessage = (event: MessageEvent<unknown>) => {
|
|
4
|
+
const request = event.data;
|
|
5
|
+
if (
|
|
6
|
+
!request ||
|
|
7
|
+
typeof request !== "object" ||
|
|
8
|
+
!("id" in request) ||
|
|
9
|
+
!("source" in request) ||
|
|
10
|
+
!("display" in request)
|
|
11
|
+
)
|
|
12
|
+
return;
|
|
13
|
+
if (typeof request.id !== "number" || typeof request.source !== "string" || typeof request.display !== "boolean")
|
|
14
|
+
return;
|
|
15
|
+
globalThis.postMessage({ id: request.id, html: renderFormula(request.source, request.display) });
|
|
16
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Minimal hast-like nodes for the streaming chunk rehype plugin. */
|
|
2
|
+
export interface HastText {
|
|
3
|
+
type: "text";
|
|
4
|
+
value: string;
|
|
5
|
+
}
|
|
6
|
+
export interface HastElement {
|
|
7
|
+
type: "element";
|
|
8
|
+
tagName: string;
|
|
9
|
+
properties?: Record<string, unknown>;
|
|
10
|
+
children: Array<HastText | HastElement>;
|
|
11
|
+
}
|
|
12
|
+
export interface HastRoot {
|
|
13
|
+
type: "root";
|
|
14
|
+
children: Array<HastText | HastElement>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const MAX_PREVIEW_LENGTH = 256_000;
|
|
2
|
+
export const MARKDOWN_PREVIEW_FRAME_NAME = "vetta-markdown-preview";
|
|
3
|
+
|
|
4
|
+
/** No same-origin, popups, forms, downloads, navigation of ancestors or host bridge. */
|
|
5
|
+
export function createPreviewDocument(source: string): string {
|
|
6
|
+
const policy = [
|
|
7
|
+
"default-src 'none'",
|
|
8
|
+
"script-src 'none'",
|
|
9
|
+
"style-src 'unsafe-inline'",
|
|
10
|
+
"img-src data: blob:",
|
|
11
|
+
"font-src data:",
|
|
12
|
+
"connect-src 'none'",
|
|
13
|
+
"frame-src 'none'",
|
|
14
|
+
"worker-src 'none'",
|
|
15
|
+
"object-src 'none'",
|
|
16
|
+
"base-uri 'none'",
|
|
17
|
+
"form-action 'none'",
|
|
18
|
+
].join("; ");
|
|
19
|
+
// The trusted outer frame also restricts child navigation. A child's own CSP alone
|
|
20
|
+
// cannot prevent scripts assigning location or a meta refresh from navigating itself.
|
|
21
|
+
const document = `<!doctype html><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="${policy}"><meta name="referrer" content="no-referrer">${source}`;
|
|
22
|
+
const escaped = document
|
|
23
|
+
.replaceAll("&", "&")
|
|
24
|
+
.replaceAll('"', """)
|
|
25
|
+
.replaceAll("<", "<")
|
|
26
|
+
.replaceAll(">", ">");
|
|
27
|
+
const outerPolicy = policy.replace("frame-src 'none'", "frame-src about:");
|
|
28
|
+
return `<!doctype html><meta http-equiv="Content-Security-Policy" content="${outerPolicy}"><style>html,body,iframe{margin:0;width:100%;height:100%;border:0}body{overflow:hidden}</style><iframe title="HTML" sandbox="" referrerpolicy="no-referrer" srcdoc="${escaped}"></iframe>`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function svgImageSource(source: string): string | null {
|
|
32
|
+
if (source.length > MAX_PREVIEW_LENGTH) return null;
|
|
33
|
+
// SVG image mode disables scripts and external resources, unlike inserting SVG into the host DOM.
|
|
34
|
+
const image = source.replace(/<svg\b([^>]*)>/i, (tag: string, attributes: string) =>
|
|
35
|
+
/\bxmlns\s*=/.test(attributes) ? tag : `<svg xmlns="http://www.w3.org/2000/svg"${attributes}>`,
|
|
36
|
+
);
|
|
37
|
+
try {
|
|
38
|
+
return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(image)}`;
|
|
39
|
+
} catch {
|
|
40
|
+
// A partial UTF-16 pair in a streamed/model-supplied string must not crash the message tree.
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export interface RichContentLabels {
|
|
2
|
+
preview: string;
|
|
3
|
+
source: string;
|
|
4
|
+
run: string;
|
|
5
|
+
stop: string;
|
|
6
|
+
waiting: string;
|
|
7
|
+
paused: string;
|
|
8
|
+
tooLarge: string;
|
|
9
|
+
failed: string;
|
|
10
|
+
staticHtml: string;
|
|
11
|
+
running: string;
|
|
12
|
+
svg: string;
|
|
13
|
+
html: string;
|
|
14
|
+
loading?: string;
|
|
15
|
+
retry?: string;
|
|
16
|
+
enlarge?: string;
|
|
17
|
+
save?: string;
|
|
18
|
+
diagram?: string;
|
|
19
|
+
imageFailed?: string;
|
|
20
|
+
actionFailed?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const defaultRichContentLabels: RichContentLabels = {
|
|
24
|
+
preview: "Preview",
|
|
25
|
+
source: "Source",
|
|
26
|
+
run: "Run JavaScript",
|
|
27
|
+
stop: "Stop",
|
|
28
|
+
waiting: "Preview will appear when generation finishes.",
|
|
29
|
+
paused: "Preview paused. Run again to restart the page.",
|
|
30
|
+
tooLarge: "This content is too large to preview. You can still view and copy its source.",
|
|
31
|
+
failed: "Could not render this content. Its source is shown instead.",
|
|
32
|
+
staticHtml: "Static preview. Run JavaScript to enable interactions; external resources are blocked.",
|
|
33
|
+
running: "Interactive preview. Automatically stops after 30 seconds.",
|
|
34
|
+
svg: "SVG preview",
|
|
35
|
+
html: "HTML preview",
|
|
36
|
+
loading: "Loading preview…",
|
|
37
|
+
retry: "Retry",
|
|
38
|
+
enlarge: "Enlarge",
|
|
39
|
+
save: "Save",
|
|
40
|
+
diagram: "Diagram",
|
|
41
|
+
imageFailed: "Could not load image.",
|
|
42
|
+
actionFailed: "Could not complete this action. Try again.",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export interface MarkdownLabels {
|
|
46
|
+
copy: string;
|
|
47
|
+
copied: string;
|
|
48
|
+
rich?: RichContentLabels;
|
|
49
|
+
}
|