Skip to content

Commit

Permalink
feat: Make marco renderer buttons adjusted flexibly to change in host…
Browse files Browse the repository at this point in the history
… domain

- showViewer button: assign data-src-link based on settings.serverLink
- syncHighlight button: depcreate data-host-link attr, dynamically use settings
- old versions still compatible
  • Loading branch information
duydl committed Aug 11, 2024
1 parent e1de2f7 commit 1734781
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
37 changes: 28 additions & 9 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ async function main() {

logseq.App.onMacroRendererSlotted(({ slot, payload }) => {
const [type, color, link] = payload.arguments
const path = extractPath(link); // tobe compatible with both old full path and new relative path
let hostLink = logseq.settings?.serverLink
// hostLink = hostLink.endsWith("/") ? hostLink.slice(0, -1) : hostLink;
hostLink = hostLink.replace(/\/$/, '')

if (type == "calibreViewer") {
// Button to Open Book
if (color == "special") {
Expand All @@ -173,7 +178,7 @@ async function main() {
slot,
template: `<button
data-on-click="showViewer"
data-src-link="${link}"
data-src-link="${hostLink + path}"
data-slot-id="${slot}"
data-block-uuid="${payload.uuid}"
class="ui__button text-center px-1 py-0 "
Expand All @@ -187,7 +192,7 @@ async function main() {
slot,
template: `<button
data-on-click="showViewer"
data-src-link="${link}"
data-src-link="${hostLink + path}"
data-slot-id="${slot}"
data-block-uuid="${payload.uuid}"
class="button"
Expand Down Expand Up @@ -235,7 +240,7 @@ async function main() {
// MacroRenderer and Model for Sync Component //

logseq.App.onMacroRendererSlotted(({ slot, payload }) => {
const [type, syncstate, interval, hostlink, lib, id, fmt] = payload.arguments
const [type, syncstate, interval, hostlink_deprecated, lib, id, fmt] = payload.arguments
if (type == "calibreHighlight") {
if (syncstate == "false") {
return logseq.provideUI({
Expand All @@ -244,7 +249,7 @@ async function main() {
template: `<button
data-on-click="update_syncstate"
data-sync-interval="${interval}"
data-host-link="${hostlink}"
data-host-link="_"
data-book-id="${lib}/${id}-${fmt}"
data-sync-state="false"
data-slot-id="${slot}"
Expand Down Expand Up @@ -288,7 +293,7 @@ async function main() {
newContent = block?.content?.replace(`${flag}`,
`{{renderer calibreHighlight, true,`);
const syncInterval = e.dataset.syncInterval
startSyncing(blockUuid, e.dataset.hostLink, e.dataset.bookId, syncInterval)
startSyncing(blockUuid, e.dataset.bookId, syncInterval)
}

if (!newContent) return
Expand All @@ -303,9 +308,11 @@ async function main() {

let intervalId; // Variable to store the interval ID. // Globally defined // Only one book syncing at a time

async function startSyncing(blockUuid, hostLink, bookId, syncInterval) {
async function startSyncing(blockUuid, bookId, syncInterval) {
// Clear any existing interval (if there is one)
clearInterval(intervalId);
let hostLink = logseq.settings?.serverLink
hostLink = hostLink.replace(/\/$/, '')
// Set the new interval
intervalId = setInterval(async () => {
await fetchUpdate(blockUuid, hostLink, bookId);
Expand Down Expand Up @@ -353,7 +360,7 @@ async function fetchUpdate(blockUuid, hostLink, bookId) {


filtered_annotations.forEach(element => {
logseq.Editor.insertBlock(blockUuid, makeBlock(element, hostLink, lib, id, format), {
logseq.Editor.insertBlock(blockUuid, makeBlock(element, lib, id, format), {
isPageBlock: false,
});
});
Expand All @@ -365,7 +372,7 @@ async function fetchUpdate(blockUuid, hostLink, bookId) {
}
}

function makeBlock(jsonObject, hostLink, lib, id, fmt) {
function makeBlock(jsonObject, lib, id, fmt) {
const {
highlighted_text,
timestamp,
Expand All @@ -382,7 +389,7 @@ function makeBlock(jsonObject, hostLink, lib, id, fmt) {
style.kind == "color" && style.type == "builtin" ? color = style.which : color = "white";
const note_add = notes ? "\n" + notes : "";

const markdownString = `{{renderer calibreViewer, ${color}, ${hostLink}/#book_id=${id}&bookpos=epubcfi%28/${(spine_index+1)*2}${start_cfi}%29&fmt=${fmt}&library_id=${lib}&mode=read_book }} ${highlighted_text}${note_add}`;
const markdownString = `{{renderer calibreViewer, ${color}, /#book_id=${id}&bookpos=epubcfi%28/${(spine_index+1)*2}${start_cfi}%29&fmt=${fmt}&library_id=${lib}&mode=read_book }} ${highlighted_text}${note_add}`;

return markdownString;
}
Expand Down Expand Up @@ -422,5 +429,17 @@ function renderSearchbar() {
logseq.showMainUI()
}

function extractPath(url) {
try {
// Attempt to create a new URL object from the input string
let parsedUrl = new URL(url);
// If successful, return the path including pathname, search, and hash
return parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
} catch (error) {
// If the URL constructor throws an error, it means the input was not a full URL
// In that case, return the original input string unchanged
return url;
}
}

logseq.useSettingsSchema(settings).ready(main).catch(console.error)
4 changes: 2 additions & 2 deletions src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ async function create(page_title, page_properties, calibre_item) {

const calibreLibrary = logseq.settings?.calibreLibrary.replace(/ /g, '_');
// Append two MacroRenderers for View and Sync in calibre-annotation Plugin
await logseq.Editor.insertBlock(newBlock.uuid, `[${calibre_item.title}](calibre://show-book/${calibreLibrary}/${calibre_item.application_id}) {{renderer calibreViewer, special, ${logseq.settings?.serverLink}/#book_id=${calibre_item.application_id}&fmt=${logseq.settings?.bookFormat}&library_id=${calibreLibrary}&mode=read_book}} {{renderer calibreHighlight, false, 2000, ${logseq.settings?.serverLink}, ${calibreLibrary}, ${calibre_item.application_id}, ${logseq.settings?.bookFormat}}}`);
await logseq.Editor.insertBlock(newBlock.uuid, `[${calibre_item.title}](calibre://show-book/${calibreLibrary}/${calibre_item.application_id}) {{renderer calibreViewer, special, /#book_id=${calibre_item.application_id}&fmt=${logseq.settings?.bookFormat}&library_id=${calibreLibrary}&mode=read_book}} {{renderer calibreHighlight, false, 2000, _, ${calibreLibrary}, ${calibre_item.application_id}, ${logseq.settings?.bookFormat}}}`);
}
else {
const newPage = await logseq.Editor.createPage(page_title, page_properties, {
Expand All @@ -281,7 +281,7 @@ async function create(page_title, page_properties, calibre_item) {

// Append two MacroRenderers for View and Sync in calibre-annotation Plugin
const calibreLibrary = logseq.settings?.calibreLibrary.replace(/ /g, '_');
await logseq.Editor.prependBlockInPage(newPage?.uuid, `[${calibre_item.title}](calibre://show-book/${calibreLibrary}/${calibre_item.application_id}) {{renderer calibreViewer, special, ${logseq.settings?.serverLink}/#book_id=${calibre_item.application_id}&fmt=${logseq.settings?.bookFormat}&library_id=${calibreLibrary}&mode=read_book}} {{renderer calibreHighlight, false, 2000, ${logseq.settings?.serverLink}, ${calibreLibrary}, ${calibre_item.application_id}, ${logseq.settings?.bookFormat}}}`);
await logseq.Editor.prependBlockInPage(newPage?.uuid, `[${calibre_item.title}](calibre://show-book/${calibreLibrary}/${calibre_item.application_id}) {{renderer calibreViewer, special, /#book_id=${calibre_item.application_id}&fmt=${logseq.settings?.bookFormat}&library_id=${calibreLibrary}&mode=read_book}} {{renderer calibreHighlight, false, 2000, _, ${calibreLibrary}, ${calibre_item.application_id}, ${logseq.settings?.bookFormat}}}`);
}

}
Expand Down

0 comments on commit 1734781

Please sign in to comment.