Skip to content

Commit 7c174dd

Browse files
committed
Prettify
1 parent 1c00d51 commit 7c174dd

1 file changed

Lines changed: 49 additions & 14 deletions

File tree

index.ts

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import '@logseq/libs';
22

33
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,
56
htmlTitleTag: /<title(\s[^>]+)*>([^<]*)<\/title>/,
67
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,
78
imageExtension: /\.(gif|jpe?g|tiff?|png|webp|bmp|tga|psd|ai)$/i,
@@ -42,7 +43,13 @@ async function getTitle(url) {
4243
return '';
4344
}
4445

45-
async function convertUrlToMarkdownLink(url, text, urlStartIndex, offset, applyFormat) {
46+
async function convertUrlToMarkdownLink(
47+
url,
48+
text,
49+
urlStartIndex,
50+
offset,
51+
applyFormat
52+
) {
4653
const title = await getTitle(url);
4754
if (title === '') {
4855
return { text, offset };
@@ -73,7 +80,7 @@ function isWrappedInCommand(text, url) {
7380
return false;
7481
}
7582

76-
return wrappedLinks.some(command => command.includes(url));
83+
return wrappedLinks.some((command) => command.includes(url));
7784
}
7885

7986
async function getFormatSettings() {
@@ -110,11 +117,26 @@ async function parseBlockForLink(uuid: string) {
110117
for (const url of urls) {
111118
const urlIndex = text.indexOf(url, offset);
112119

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+
) {
114130
continue;
115131
}
116132

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+
);
118140
text = updatedTitle.text;
119141
offset = updatedTitle.offset;
120142
}
@@ -165,38 +187,51 @@ const main = async () => {
165187
for (let i = 0; i < mutationsList.length; i++) {
166188
const addedNode = mutationsList[i].addedNodes[0];
167189
if (addedNode && addedNode.childNodes.length) {
168-
const extLinkList = addedNode.querySelectorAll('.external-link');
190+
const extLinkList =
191+
addedNode.querySelectorAll('.external-link');
169192
if (extLinkList.length) {
170193
extLinksObserver.disconnect();
171194
for (let i = 0; i < extLinkList.length; i++) {
172195
setFavicon(extLinkList[i]);
173196
}
174197

175-
extLinksObserver.observe(appContainer, extLinksObserverConfig);
198+
extLinksObserver.observe(
199+
appContainer,
200+
extLinksObserverConfig
201+
);
176202
}
177203
}
178204
}
179205
});
180206

181207
setTimeout(() => {
182-
doc.querySelectorAll('.external-link')?.forEach(extLink => setFavicon(extLink));
208+
doc.querySelectorAll('.external-link')?.forEach((extLink) =>
209+
setFavicon(extLink)
210+
);
183211
extLinksObserver.observe(appContainer, extLinksObserverConfig);
184212
}, 500);
185213

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+
);
191223

192224
const blockSet = new Set();
193225
logseq.DB.onChanged(async (e) => {
194226
if (e.txMeta?.outlinerOp !== 'insertBlocks') {
195227
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+
);
197231
return;
198232
}
199233

234+
console.log('e', e);
200235
await blockSet.forEach((uuid) => parseBlockForLink(uuid));
201236
blockSet.clear();
202237
});

0 commit comments

Comments
 (0)