chore(clipper): convert relative links to absolute links so they don't become relative to SN's url

This commit is contained in:
Aman Harwara
2023-05-16 22:20:31 +05:30
parent fc06d0001a
commit 8ba290dbac
2 changed files with 27 additions and 4 deletions

View File

@@ -1,9 +1,12 @@
import { $createParagraphNode, $getRoot, $insertNodes, LexicalNode } from 'lexical'
import { $createParagraphNode, $getRoot, $insertNodes, $nodesOfType, LexicalNode } from 'lexical'
import { $generateNodesFromDOM } from '@lexical/html'
import { createHeadlessEditor } from '@lexical/headless'
import { BlockEditorNodes } from '../SuperEditor/Lexical/Nodes/AllNodes'
import BlocksEditorTheme from '../SuperEditor/Lexical/Theme/Theme'
import { ClipPayload } from '@standardnotes/clipper/src/types/message'
import { LinkNode } from '@lexical/link'
const AbsoluteLinkRegExp = new RegExp('^(?:[a-z+]+:)?//', 'i')
export const getSuperJSONFromClipPayload = async (clipPayload: ClipPayload) => {
const editor = createHeadlessEditor({
@@ -14,6 +17,8 @@ export const getSuperJSONFromClipPayload = async (clipPayload: ClipPayload) => {
nodes: [...BlockEditorNodes],
})
const clipURL = new URL(clipPayload.url)
await new Promise<void>((resolve) => {
editor.update(() => {
const parser = new DOMParser()
@@ -59,5 +64,21 @@ export const getSuperJSONFromClipPayload = async (clipPayload: ClipPayload) => {
})
})
await new Promise<void>((resolve) => {
editor.update(() => {
$nodesOfType(LinkNode).forEach((linkNode) => {
const url = linkNode.getURL()
const isAbsoluteLink = AbsoluteLinkRegExp.test(url)
if (!isAbsoluteLink) {
const fixedURL = new URL(url, clipURL)
linkNode.setURL(fixedURL.toString())
}
})
resolve()
})
})
return JSON.stringify(editor.getEditorState().toJSON())
}

View File

@@ -30,9 +30,11 @@ const ItemSelectionDropdown = ({ contentTypes, placeholder, onSelection, combobo
const combobox = useComboboxStore(comboboxProps)
const value = combobox.useState('value')
const open = combobox.useState('open')
if (value.length < 1 && open) {
combobox.setOpen(false)
}
useEffect(() => {
if (value.length < 1 && open) {
combobox.setOpen(false)
}
}, [combobox, open, value.length])
const searchQuery = useDeferredValue(value)
const [items, setItems] = useState<DecryptedItem[]>([])