Add notebook preview support (#1725)
* Add notebook preview support * Fix notebook reopen and browser routing * Add editable runnable notebooks * Restore notebook highlighting and defer source serialization * Mark notebook editor beta * Fix notebook Python syntax highlighting * Improve notebook cell controls and dark editing * Fix notebook focus and save shortcuts * Fix restored notebook file loads * Harden notebook perf and output rendering * Fix notebook source toggle shutdown noise * Harden notebook execution trust prompt
This commit is contained in:
@@ -35,6 +35,15 @@ describe('browser-url helpers', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('normalizes pasted absolute local paths to file URLs', () => {
|
||||
expect(normalizeBrowserNavigationUrl('/Users/me/Downloads/Example.ipynb')).toBe(
|
||||
'file:///Users/me/Downloads/Example.ipynb'
|
||||
)
|
||||
expect(normalizeBrowserNavigationUrl('C:\\Users\\me\\Downloads\\Example.ipynb')).toBe(
|
||||
'file:///C:/Users/me/Downloads/Example.ipynb'
|
||||
)
|
||||
})
|
||||
|
||||
// Why: in-app preview is fine (sandboxed webview), but handing file:// to
|
||||
// shell.openExternal would let a remote page drive Finder/Explorer to
|
||||
// arbitrary paths. External-open paths must still refuse file://.
|
||||
|
||||
@@ -8,6 +8,8 @@ const LOCAL_ADDRESS_PATTERN =
|
||||
// A single-word input containing a dot with a valid TLD-like suffix is treated as
|
||||
// a URL attempt, not a search query.
|
||||
const LOOKS_LIKE_URL_PATTERN = /^[^\s]+\.[a-z]{2,}(\/.*)?$/i
|
||||
const WINDOWS_ABSOLUTE_PATH_PATTERN = /^[A-Za-z]:[\\/][^\s]*$/
|
||||
const UNIX_ABSOLUTE_PATH_PATTERN = /^\/[^\s]*$/
|
||||
|
||||
export type SearchEngine = 'google' | 'duckduckgo' | 'bing' | 'kagi'
|
||||
|
||||
@@ -131,6 +133,19 @@ export function looksLikeSearchQuery(input: string): boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
function absolutePathToFileUrl(filePath: string): string {
|
||||
const normalizedPath = filePath.replaceAll('\\', '/')
|
||||
const segments = normalizedPath.split('/').map((segment, index) => {
|
||||
if (index === 0 && /^[A-Za-z]:$/.test(segment)) {
|
||||
return segment
|
||||
}
|
||||
return encodeURIComponent(segment)
|
||||
})
|
||||
return normalizedPath.startsWith('/')
|
||||
? `file://${segments.join('/')}`
|
||||
: `file:///${segments.join('/')}`
|
||||
}
|
||||
|
||||
export function normalizeBrowserNavigationUrl(
|
||||
rawUrl: string,
|
||||
searchEngine?: SearchEngine | null,
|
||||
@@ -149,6 +164,10 @@ export function normalizeBrowserNavigationUrl(
|
||||
}
|
||||
}
|
||||
|
||||
if (UNIX_ABSOLUTE_PATH_PATTERN.test(trimmed) || WINDOWS_ABSOLUTE_PATH_PATTERN.test(trimmed)) {
|
||||
return absolutePathToFileUrl(trimmed)
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = new URL(trimmed)
|
||||
// Why: file:// is allowed so the browser pane can render local files the
|
||||
|
||||
Reference in New Issue
Block a user