top of page

How to export all blocks in logseq for a specific tag (even from linked pages)

I have a love-hate relationship with Logseq! How amazing a tool it can be but how stupid it can be at times w.r.t not having export feature for linked blocks. Alas the mental block is too big in my head to shift to Obsydian or other tool. Anyways, moving on to task:


Task: Download all blocks linked with a specific tag

Eg: I want to download all block I have linked with "#question". On specific days in journal, I tag something with "#question". Or when I am working on a project, I tag it with "#question".

I want to download all of them in a markdown file to be put in notion.


Step 1: Open Developer Console in Logseq App

I am on mac. Select Logseq native app and then press "Command + Option + I" (last character is letter I ~ I as in "India")


Step 2: Paste the below script in Console & run it

  • PS: edit the TAG_TO_SEARCH variable on line 1. Whatever tag you want to download.

// The tag you want to search for and remove from content
const TAG_TO_SEARCH = "question";

const results = window.logseq.api.datascript_query(`
  [:find (pull ?b [:block/content :block/created-at 
                   {:block/page [:block/name :block/created-at :block/journal-day 
                                 :block/journal? :block/properties]}])
   :where
   [?b :block/refs ?p1]
   [?p1 :block/name "${TAG_TO_SEARCH}"]]
`);

function formatDate(epoch) {
  if (!epoch) return 'Date not available';
  const date = new Date(epoch);
  return date.toLocaleDateString('en-US', { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric'
  });
}

function formatJournalDay(journalDay) {
  if (!journalDay) return 'Not a journal page';
  const year = Math.floor(journalDay / 10000);
  const month = Math.floor((journalDay % 10000) / 100) - 1; // JavaScript months are 0-indexed
  const day = journalDay % 100;
  return new Date(year, month, day).toLocaleDateString('en-US', { 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric' 
  });
}


let formattedResults1 = results.map(result => {
  const block = Array.isArray(result) ? result[0] : result;
  let createdAt = block.page?.['created-at'];
  return {
    content: block.content,
    page: {
      name: block.page?.name || 'Unnamed Page',
      createdAt: formatDate(createdAt),
      readableDate: formatDate(createdAt),
      isJournal: block.page?.['journal?'] || false,
      journalDay: formatJournalDay(block.page?.['journal-day']),
      properties: block.page?.properties || {}
    },
    epochCreatedAt: createdAt
  };
});

formattedResults1.sort((a,b)=>{return b.epochCreatedAt - a.epochCreatedAt;})

console.log(JSON.stringify(formattedResults1, null, 2));

// Create markdown content
let markdown = ``;
formattedResults1.forEach((block, index) => {
  markdown += `${block['page']['readableDate']}\n`;
  markdown += `${block.content}\n`;
  markdown += "---\n\n";
});

// Create a Blob with the markdown content
const blob = new Blob([markdown], { type: 'text/markdown;charset=utf-8;' });

// Create a download link
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${TAG_TO_SEARCH}_blocks.md`;

// Trigger the download
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

console.log("Download initiated. Check your downloads folder.");

Comments


bottom of page