Skip to content

Commit

Permalink
add slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
ab authored and ab committed Feb 6, 2024
1 parent 342c505 commit e7c2db1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
1 change: 1 addition & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
### 演示

![demo](./demo.gif)
![demo](./demo1.gif)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Converting a paseted URL copyed from a browser into markdown format and automati
### Demo

![demo](./demo.gif)
![demo](./demo1.gif)
Binary file added demo1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 26 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// TODO convert to ts
async function getLinkTitle(link) {
return await fetch(link, {
headers: {
Expand Down Expand Up @@ -29,36 +28,40 @@ function isMarkdownURL(content) {
: null;
}

async function main() {
logseq.provideModel({
async updateBlock() {
const currentBlock = await logseq.Editor.getCurrentBlock();
let content = currentBlock?.content;
let blockUuid = currentBlock?.uuid;
async function 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;
}
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.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.");
}
}
}

async function main() {
logseq.provideModel({
updateBlock,
});

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.Editor.registerSlashCommand("get link title", updateBlock);
}

logseq.ready(main).catch(console.error);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "logseq-get-link-title",
"version": "0.0.1",
"version": "0.0.2",
"description": "Automatically fetching title of a hyperlink, and converting it into the markdown format.",
"main": "dist/index.html",
"targets": {
Expand All @@ -13,7 +13,7 @@
"url": "https://github.com/abcvav/logseq-get-link-title.git"
},
"dependencies": {
"@logseq/libs": "^0.0.15"
"@logseq/libs": "^0.0.17"
},
"scripts": {
"build": "parcel build --no-source-maps index.html --public-url ./",
Expand Down
14 changes: 10 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@
resolved "https://registry.npmjs.org/@lmdb/lmdb-win32-x64/-/lmdb-win32-x64-2.8.5.tgz"
integrity sha512-4wvrf5BgnR8RpogHhtpCPJMKBmvyZPhhUtEwMJbXh0ni2BucpfF07jlmyM11zRqQ2XIq6PbC2j7W7UCCcm1rRQ==

"@logseq/libs@^0.0.15":
version "0.0.15"
resolved "https://registry.npmjs.org/@logseq/libs/-/libs-0.0.15.tgz"
integrity sha512-Z4YrYGfu8Y3s9LTqVnPGkxlO+KZtP1YalH/A63zYgxP61cV5QtKlnHWNcjsKxsD5CkaSL4MlSN4mf7wNx/Fm0A==
"@logseq/libs@^0.0.17":
version "0.0.17"
resolved "https://registry.yarnpkg.com/@logseq/libs/-/libs-0.0.17.tgz#245cdabc2d1bc3ec6164d406349c5c649a040031"
integrity sha512-SkzzAaocmrgeHYrCOaRyEqzPOxw3d0qVEZSrt9qVvXE4tuEgbvEHR8tzI1N5RjgAv+PDWuGPiP7/mhcXHpINEw==
dependencies:
csstype "3.1.0"
debug "4.3.4"
deepmerge "4.3.1"
dompurify "2.3.8"
eventemitter3 "4.0.7"
fast-deep-equal "3.1.3"
Expand Down Expand Up @@ -1043,6 +1044,11 @@ [email protected]:
dependencies:
ms "2.1.2"

[email protected]:
version "4.3.1"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.3.1.tgz#44b5f2147cd3b00d4b56137685966f26fd25dd4a"
integrity sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==

detect-libc@^1.0.3:
version "1.0.3"
resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz"
Expand Down

0 comments on commit e7c2db1

Please sign in to comment.