Skip to content

Commit 67c71bc

Browse files
committed
Add website preview functionality using marked for markdown rendering
1 parent 53cdd9f commit 67c71bc

15 files changed

Lines changed: 103 additions & 8 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,7 @@
19861986
"he": "^1.2.0",
19871987
"ignore": "^6.0.2",
19881988
"jsdom": "^24.0.0",
1989+
"marked": "^17.0.1",
19891990
"path-browserify": "^1.0.1",
19901991
"react": "^18.2.0",
19911992
"react-dom": "^18.2.0",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as vscode from 'vscode'
2+
import { OpenWebsiteMessage } from '../../types/messages'
3+
import * as fs from 'fs'
4+
import { get_website_file_path } from '../utils/website-fetcher'
5+
import { marked } from 'marked'
6+
7+
export const handle_open_website = async (message: OpenWebsiteMessage) => {
8+
const file_path = get_website_file_path(message.url)
9+
10+
if (fs.existsSync(file_path)) {
11+
const markdown = fs.readFileSync(file_path, 'utf8')
12+
const html = await marked.parse(markdown)
13+
14+
const panel = vscode.window.createWebviewPanel(
15+
'websitePreview',
16+
'Website Preview',
17+
vscode.ViewColumn.One
18+
)
19+
20+
panel.webview.html = `
21+
<!DOCTYPE html>
22+
<html lang="en">
23+
<head>
24+
<meta charset="UTF-8">
25+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
26+
<title>Website Preview</title>
27+
<style>
28+
body {
29+
line-height: 1.5;
30+
padding: 20px 40px;
31+
}
32+
hr {
33+
height: 1px;
34+
border: none;
35+
background-color: var(--vscode-editorWidget-border);
36+
}
37+
</style>
38+
</head>
39+
<body>
40+
<a href="${message.url}">${message.url}</a>
41+
<hr>
42+
${html}
43+
</body>
44+
</html>
45+
`
46+
} else {
47+
vscode.env.openExternal(vscode.Uri.parse(message.url))
48+
}
49+
}

‎apps/editor/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ export * from './handle-save-prompt-document'
6767
export * from './handle-open-prompt-document'
6868
export * from './handle-paste-url'
6969
export * from './handle-voice-input'
70+
export * from './handle-open-website'

‎apps/editor/src/views/panel/backend/panel-provider.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ import {
7373
handle_delete_task,
7474
handle_fix_all_failed_files,
7575
handle_prune_context,
76-
handle_cancel_intelligent_update_file_in_preview,
7776
handle_get_prune_context_instructions_prefix,
7877
handle_save_prune_context_instructions_prefix,
7978
handle_open_external_url,
@@ -84,7 +83,9 @@ import {
8483
handle_save_prompt_document,
8584
handle_open_prompt_document,
8685
handle_paste_url,
87-
handle_voice_input
86+
handle_voice_input,
87+
handle_open_website,
88+
handle_cancel_intelligent_update_file_in_preview
8889
} from './message-handlers'
8990
import { SelectionState } from '../types/messages'
9091
import {
@@ -845,6 +846,8 @@ export class PanelProvider implements vscode.WebviewViewProvider {
845846
await handle_open_prompt_document(message)
846847
} else if (message.command == 'PASTE_URL') {
847848
await handle_paste_url(this, message)
849+
} else if (message.command == 'OPEN_WEBSITE') {
850+
await handle_open_website(message)
848851
} else if (message.command == 'SET_RECORDING_STATE') {
849852
await handle_voice_input(this, message)
850853
} else if (message.command == 'GET_SETUP_PROGRESS') {

‎apps/editor/src/views/panel/backend/utils/website-fetcher.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ const create_turndown_service = () => {
8585
filter: ['figure', 'picture', 'sup'],
8686
replacement: () => ''
8787
})
88+
turndown_service.addRule('removeLinks', {
89+
filter: 'a',
90+
replacement: (content) => content
91+
})
8892
return turndown_service
8993
}
9094

‎apps/editor/src/views/panel/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type Props = {
6464
prune_context_instructions_prefix: string
6565
on_prune_context_instructions_prefix_change: (prefix: string) => void
6666
on_open_url: (url: string) => void
67+
on_open_website: (url: string) => void
6768
on_paste_image: (base64_content: string) => void
6869
on_open_image: (hash: string) => void
6970
on_paste_document: (text: string) => void
@@ -848,6 +849,7 @@ export const Main: React.FC<Props> = (props) => {
848849
props.on_prune_context_instructions_prefix_change
849850
}
850851
on_open_url={props.on_open_url}
852+
on_open_website={props.on_open_website}
851853
on_paste_image={props.on_paste_image}
852854
on_open_image={props.on_open_image}
853855
on_paste_document={props.on_paste_document}

‎apps/editor/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ type Props = {
102102
on_go_to_file: (file_path: string) => void
103103
on_pasted_lines_click: (path: string, start?: string, end?: string) => void
104104
on_open_url: (url: string) => void
105+
on_open_website: (url: string) => void
105106
are_keyboard_shortcuts_disabled: boolean
106107
prune_context_instructions_prefix: string
107108
on_prune_context_instructions_prefix_change: (prefix: string) => void
@@ -268,6 +269,7 @@ export const MainView: React.FC<Props> = (props) => {
268269
on_go_to_file={props.on_go_to_file}
269270
on_pasted_lines_click={props.on_pasted_lines_click}
270271
on_open_url={props.on_open_url}
272+
on_open_website={props.on_open_website}
271273
invocation_count={current_invocation_count}
272274
on_invocation_count_change={handle_invocation_count_change}
273275
prune_context_instructions_prefix={

‎apps/editor/src/views/panel/frontend/Panel.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ export const Panel = () => {
222222
})
223223
}
224224

225+
const handle_open_website = (url: string) => {
226+
post_message(vscode, {
227+
command: 'OPEN_WEBSITE',
228+
url
229+
})
230+
}
231+
225232
const layout_context_value = {
226233
can_undo,
227234
on_apply_click: handle_apply_click,
@@ -312,6 +319,7 @@ export const Panel = () => {
312319
handle_prune_context_instructions_prefix_change
313320
}
314321
on_open_url={handle_open_url}
322+
on_open_website={handle_open_website}
315323
on_paste_image={handle_paste_image}
316324
on_open_image={handle_open_image}
317325
on_paste_document={handle_paste_long_document}

‎apps/editor/src/views/panel/types/messages.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ export interface OpenExternalUrlMessage extends BaseMessage {
478478
url: string
479479
}
480480

481+
export interface OpenWebsiteMessage extends BaseMessage {
482+
command: 'OPEN_WEBSITE'
483+
url: string
484+
}
485+
481486
export interface UpsertConfigurationMessage extends BaseMessage {
482487
command: 'UPSERT_CONFIGURATION'
483488
tool_type: ToolType
@@ -619,6 +624,7 @@ export type FrontendMessage =
619624
| SavePromptDocumentMessage
620625
| OpenPromptDocumentMessage
621626
| PasteUrlMessage
627+
| OpenWebsiteMessage
622628
| SetRecordingStateMessage
623629
| GetSetupProgressMessage
624630

‎apps/editor/src/views/settings/backend/message-handlers/handle-edit-model-provider-key.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const handle_change_model_provider_key = async (
3232
password: true,
3333
placeHolder: provider_to_update.api_key
3434
? `...${provider_to_update.api_key.slice(-4)}`
35-
: 'No API key set'
35+
: ''
3636
})
3737

3838
if (new_api_key === undefined) {

0 commit comments

Comments
 (0)