|
1 | 1 | import '@logseq/libs'; |
2 | 2 |
|
3 | 3 | const DEFAULT_REGEX = { |
4 | | - wrappedInCommand: /(\{\{(video)\s*(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})\s*\}\})/gi, |
| 4 | + wrappedInCommand: |
| 5 | + /(\{\{(video)\s*(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})\s*\}\})/gi, |
5 | 6 | htmlTitleTag: /<title(\s[^>]+)*>([^<]*)<\/title>/, |
6 | 7 | line: /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi, |
7 | 8 | imageExtension: /\.(gif|jpe?g|tiff?|png|webp|bmp|tga|psd|ai)$/i, |
@@ -42,7 +43,13 @@ async function getTitle(url) { |
42 | 43 | return ''; |
43 | 44 | } |
44 | 45 |
|
45 | | -async function convertUrlToMarkdownLink(url, text, urlStartIndex, offset, applyFormat) { |
| 46 | +async function convertUrlToMarkdownLink( |
| 47 | + url, |
| 48 | + text, |
| 49 | + urlStartIndex, |
| 50 | + offset, |
| 51 | + applyFormat |
| 52 | +) { |
46 | 53 | const title = await getTitle(url); |
47 | 54 | if (title === '') { |
48 | 55 | return { text, offset }; |
@@ -73,7 +80,7 @@ function isWrappedInCommand(text, url) { |
73 | 80 | return false; |
74 | 81 | } |
75 | 82 |
|
76 | | - return wrappedLinks.some(command => command.includes(url)); |
| 83 | + return wrappedLinks.some((command) => command.includes(url)); |
77 | 84 | } |
78 | 85 |
|
79 | 86 | async function getFormatSettings() { |
@@ -110,11 +117,26 @@ async function parseBlockForLink(uuid: string) { |
110 | 117 | for (const url of urls) { |
111 | 118 | const urlIndex = text.indexOf(url, offset); |
112 | 119 |
|
113 | | - if (isAlreadyFormatted(text, url, urlIndex, formatSettings.formatBeginning) || isImage(url) || isWrappedInCommand(text, url)) { |
| 120 | + if ( |
| 121 | + isAlreadyFormatted( |
| 122 | + text, |
| 123 | + url, |
| 124 | + urlIndex, |
| 125 | + formatSettings.formatBeginning |
| 126 | + ) || |
| 127 | + isImage(url) || |
| 128 | + isWrappedInCommand(text, url) |
| 129 | + ) { |
114 | 130 | continue; |
115 | 131 | } |
116 | 132 |
|
117 | | - const updatedTitle = await convertUrlToMarkdownLink(url, text, urlIndex, offset, formatSettings.applyFormat); |
| 133 | + const updatedTitle = await convertUrlToMarkdownLink( |
| 134 | + url, |
| 135 | + text, |
| 136 | + urlIndex, |
| 137 | + offset, |
| 138 | + formatSettings.applyFormat |
| 139 | + ); |
118 | 140 | text = updatedTitle.text; |
119 | 141 | offset = updatedTitle.offset; |
120 | 142 | } |
@@ -165,38 +187,51 @@ const main = async () => { |
165 | 187 | for (let i = 0; i < mutationsList.length; i++) { |
166 | 188 | const addedNode = mutationsList[i].addedNodes[0]; |
167 | 189 | if (addedNode && addedNode.childNodes.length) { |
168 | | - const extLinkList = addedNode.querySelectorAll('.external-link'); |
| 190 | + const extLinkList = |
| 191 | + addedNode.querySelectorAll('.external-link'); |
169 | 192 | if (extLinkList.length) { |
170 | 193 | extLinksObserver.disconnect(); |
171 | 194 | for (let i = 0; i < extLinkList.length; i++) { |
172 | 195 | setFavicon(extLinkList[i]); |
173 | 196 | } |
174 | 197 |
|
175 | | - extLinksObserver.observe(appContainer, extLinksObserverConfig); |
| 198 | + extLinksObserver.observe( |
| 199 | + appContainer, |
| 200 | + extLinksObserverConfig |
| 201 | + ); |
176 | 202 | } |
177 | 203 | } |
178 | 204 | } |
179 | 205 | }); |
180 | 206 |
|
181 | 207 | setTimeout(() => { |
182 | | - doc.querySelectorAll('.external-link')?.forEach(extLink => setFavicon(extLink)); |
| 208 | + doc.querySelectorAll('.external-link')?.forEach((extLink) => |
| 209 | + setFavicon(extLink) |
| 210 | + ); |
183 | 211 | extLinksObserver.observe(appContainer, extLinksObserverConfig); |
184 | 212 | }, 500); |
185 | 213 |
|
186 | | - logseq.Editor.registerBlockContextMenuItem('Format url titles', async ({ uuid }) => { |
187 | | - await parseBlockForLink(uuid); |
188 | | - const extLinkList: NodeListOf<HTMLAnchorElement> = doc.querySelectorAll('.external-link'); |
189 | | - extLinkList.forEach(extLink => setFavicon(extLink)); |
190 | | - }); |
| 214 | + logseq.Editor.registerBlockContextMenuItem( |
| 215 | + 'Format url titles', |
| 216 | + async ({ uuid }) => { |
| 217 | + await parseBlockForLink(uuid); |
| 218 | + const extLinkList: NodeListOf<HTMLAnchorElement> = |
| 219 | + doc.querySelectorAll('.external-link'); |
| 220 | + extLinkList.forEach((extLink) => setFavicon(extLink)); |
| 221 | + } |
| 222 | + ); |
191 | 223 |
|
192 | 224 | const blockSet = new Set(); |
193 | 225 | logseq.DB.onChanged(async (e) => { |
194 | 226 | if (e.txMeta?.outlinerOp !== 'insertBlocks') { |
195 | 227 | blockSet.add(e.blocks[0]?.uuid); |
196 | | - doc.querySelectorAll('.external-link')?.forEach(extLink => setFavicon(extLink)); |
| 228 | + doc.querySelectorAll('.external-link')?.forEach((extLink) => |
| 229 | + setFavicon(extLink) |
| 230 | + ); |
197 | 231 | return; |
198 | 232 | } |
199 | 233 |
|
| 234 | + console.log('e', e); |
200 | 235 | await blockSet.forEach((uuid) => parseBlockForLink(uuid)); |
201 | 236 | blockSet.clear(); |
202 | 237 | }); |
|
0 commit comments