Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
abcvav committed Nov 19, 2023
0 parents commit dc12489
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
Binary file added Link.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Description

Converting a paseted URL copyed from a browser into markdown format and automatically retrieving the title.

### Demo

![demo](./demo.gif)
Binary file added demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Hello World LSPlugin :)</title>
</head>
<body>
<div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/@logseq/libs"></script>
<script src="./index.js"></script>
</body>
</html>
63 changes: 63 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
async function getLinkTitle(link) {
return await fetch(link, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
Accept: "text/html",
},
})
.then((res) => {
if (!res.ok) {
throw new Error("fetch error");
}
return res.text();
})
.then((text) => {
let matchedGroup = text.match(/<title.*?>(.*?)<\/title>/);
return matchedGroup ? matchedGroup[1] : "";
})
.catch((e) => {
logseq.UI.showMsg("Connection timeout.");
});
}

function isMarkdownURL(content) {
let markdownURL = content.match(/\[.*?\]\((.*)?\)/);
return markdownURL && markdownURL[1] ? markdownURL[1] : null;
}

async function main() {
logseq.provideModel({
async updateBlock() {
const currentBlock = await logseq.Editor.getCurrentBlock();
let content = currentBlock?.content;
let blockUuid = currentBlock?.uuid;

if (content) {
let markdownURL = isMarkdownURL(content);
if (markdownURL) {
await logseq.Editor.updateBlock(blockUuid, markdownURL);
return;
}

logseq.UI.showMsg("🚀 Fetching title...");
let title = await getLinkTitle(content);
if (title) {
let newContent = `[${title}](${content})`;
await logseq.Editor.updateBlock(blockUuid, newContent);
} else {
logseq.UI.showMsg("No title get.");
}
}
},
});

logseq.App.registerUIItem("toolbar", {
key: "get-link-title",
template: `<a data-on-click="updateBlock" class="button"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-link-45deg" viewBox="0 0 16 16"><path width="18" d="M4.715 6.542 3.343 7.914a3 3 0 1 0 4.243 4.243l1.828-1.829A3 3 0 0 0 8.586 5.5L8 6.086a1.002 1.002 0 0 0-.154.199 2 2 0 0 1 .861 3.337L6.88 11.45a2 2 0 1 1-2.83-2.83l.793-.792a4.018 4.018 0 0 1-.128-1.287z"/><path width="18" d="M6.586 4.672A3 3 0 0 0 7.414 9.5l.775-.776a2 2 0 0 1-.896-3.346L9.12 3.55a2 2 0 1 1 2.83 2.83l-.793.792c.112.42.155.855.128 1.287l1.372-1.372a3 3 0 1 0-4.243-4.243L6.586 4.672z"/></svg></a>`,
});

logseq.UI.showMsg("hello world");
}

logseq.ready(main).catch(console.error);
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "logseq-get-link-title",
"version": "0.0.1",
"description": "Automatically fetching title of a web link when pasted to block, and converting it into a hyperlink of markdown format ",
"main": "index.html",
"author": "Jialin Luo",
"dependencies": {
"@logseq/libs": "^0.0.15"
},
"logseq": {
"id": "_9kot8cf9c",
"icon": "Link.png"
}
}

0 comments on commit dc12489

Please sign in to comment.