Skip to content

Commit

Permalink
Merge pull request #6 from sawhney17/main
Browse files Browse the repository at this point in the history
fix non persistent block refs
  • Loading branch information
sidharth-panwar authored May 31, 2022
2 parents 513668b + 9c76831 commit de81906
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 139 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# logseq-extract-plugin
A fork of the [logseq extract plugin](https://github.com/sidharth-panwar/logseq-extract-plugin) by @sidharth-panwar
The logseq-extract-plugin is used to extract bold and highlighted text from a block and all its nested blocks or the complete page.

Here's how it looks like:
Expand All @@ -24,5 +25,4 @@ Whenever changing the settings, you'll need to restart logseq for settings to ta

## Running the Plugin

- `yarn install && yarn build` in terminal to install dependencies.
- `Load unpacked plugin` in Logseq Desktop client.
- Download the release
267 changes: 132 additions & 135 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,161 +1,158 @@
function manageSettings(logseq) {

//Default settings - Extracts highlights and bold items
const highlightsRegEx = '(==[^=]+==)';
const boldRegEx = '(\\*\\*[^\\*]+\\*\\*)';

//CHANGE THIS WHEN ADDING NEW SETTINGS
const settingsVersion = 'v1';

//Use only for saving these first time in the plugin settings file
let defaultSettings = {
expr: `${highlightsRegEx}|${boldRegEx}`,
summaryTitle: 'Summary',
keepRefs: true, //Keeps a reference to the source block in this format [*](((uuid)))
nested: true, //Extract from current block and all the child blocks
keepMeta: false, //Remove highlights and bold
settingsVersion: settingsVersion
};

//Load plugin settings
let pluginSettings = logseq.settings;

//This is to ensure that in future if new settings are added they are written over to settings file.
//This will overwrite user's existing settings though.
const shouldUpdateSettings = pluginSettings.settingsVersion != defaultSettings.settingsVersion;

//If first time, then save default settings
if (shouldUpdateSettings) {

pluginSettings = defaultSettings;
logseq.updateSettings(pluginSettings);
}
//Default settings - Extracts highlights and bold items
const highlightsRegEx = "(==[^=]+==)";
const boldRegEx = "(\\*\\*[^\\*]+\\*\\*)";

//CHANGE THIS WHEN ADDING NEW SETTINGS
const settingsVersion = "v1";

//Use only for saving these first time in the plugin settings file
let defaultSettings = {
expr: `${highlightsRegEx}|${boldRegEx}`,
summaryTitle: "Summary",
keepRefs: true, //Keeps a reference to the source block in this format [*](((uuid)))
nested: true, //Extract from current block and all the child blocks
keepMeta: false, //Remove highlights and bold
settingsVersion: settingsVersion,
};

//Load plugin settings
let pluginSettings = logseq.settings;

//This is to ensure that in future if new settings are added they are written over to settings file.
//This will overwrite user's existing settings though.
const shouldUpdateSettings =
pluginSettings.settingsVersion != defaultSettings.settingsVersion;

//If first time, then save default settings
if (shouldUpdateSettings) {
pluginSettings = defaultSettings;
logseq.updateSettings(pluginSettings);
}
}

function main() {
manageSettings(logseq);

const pluginSettings = logseq.settings;

//targetBlock is the block under which the summary will be created.
//For block extract this will be immediately below the current block.
//For page extract it'll be the last block in the page.
var summarizeExtracts = async (extracts, targetBlock) => {
//Create a summary block below the current block (sibling) - you can change content of this block
//from Summary to something else by changing the summaryTitle property in settings
var summaryBlock = await logseq.Editor.insertBlock(
targetBlock.uuid,
pluginSettings.summaryTitle,
{ sibling: true }
);

manageSettings(logseq);

const pluginSettings = logseq.settings;

//targetBlock is the block under which the summary will be created.
//For block extract this will be immediately below the current block.
//For page extract it'll be the last block in the page.
var summarizeExtracts = async (extracts, targetBlock) => {

//Create a summary block below the current block (sibling) - you can change content of this block
//from Summary to something else by changing the summaryTitle property in settings
var summaryBlock = await logseq.Editor.insertBlock(targetBlock.uuid, pluginSettings.summaryTitle, { sibling: true });

//Create the extracts as children blocks of summary block
extracts.forEach((i) => {

let content = i.content;

//Remove == or ** from start and end if keepMeta is false
content = pluginSettings.keepMeta ? content : content.slice(2, -2);

//Keep reference of source block
content = pluginSettings.keepRefs ? `${content} [*](((${i.source.uuid})))` : content;

logseq.Editor.insertBlock(summaryBlock.uuid, content, { sibling: false });
});

logseq.App.showMsg('✔️ Extraction completed successfully!');
};

var processBlock = async (currentBlock) => {

//Get current block content
const block = await logseq.Editor.getBlock(currentBlock.uuid, { includeChildren: pluginSettings.nested });
const regEx = new RegExp(pluginSettings.expr, 'g');

//Get all extracts that match regex
//const extracts = [...block.content.matchAll(regEx)];
let extracts = [];
getExtracts(block, regEx, extracts);

return extracts;
//Create the extracts as children blocks of summary block
extracts.forEach((i) => {
let content = i.content;

//Remove == or ** from start and end if keepMeta is false
content = pluginSettings.keepMeta ? content : content.slice(2, -2);

//Keep reference of source block
content = pluginSettings.keepRefs
? `${content} [*](((${i.source.uuid})))`
: content;
if (!i.source.properties?.id) {
logseq.Editor.upsertBlockProperty(i.source.uuid, "id", i.source.uuid);
}

logseq.Editor.insertBlock(summaryBlock.uuid, content, { sibling: false });
});

logseq.App.showMsg("✔️ Extraction completed successfully!");
};

var processBlock = async (currentBlock) => {
//Get current block content
const block = await logseq.Editor.getBlock(currentBlock.uuid, {
includeChildren: pluginSettings.nested,
});
const regEx = new RegExp(pluginSettings.expr, "g");

//Get all extracts that match regex
//const extracts = [...block.content.matchAll(regEx)];
let extracts = [];
getExtracts(block, regEx, extracts);
//if extracts is empty then return

return extracts;
};

//blockPipeline is the entry point when we extract at block level.
var blockPipeline = async (currentBlock) => {
let extracts = await processBlock(currentBlock);

//EXIT if no extracts found
if (!extracts || !extracts.length) {
logseq.App.showMsg("❗ Nothing to extract!");
return;
}

//blockPipeline is the entry point when we extract at block level.
var blockPipeline = async (currentBlock) => {

let extracts = await processBlock(currentBlock);

//EXIT if no extracts found
if (!extracts || !extracts.length) {

logseq.App.showMsg('❗ Nothing to extract!');
return;
}

summarizeExtracts(extracts, currentBlock);
};

//blockPipeline is the entry point when we extract at page level.
var pagePipeline = async (context) => {
summarizeExtracts(extracts, currentBlock);
};

let pageBlocks = await logseq.Editor.getPageBlocksTree(context.page);
let extracts = [];
//blockPipeline is the entry point when we extract at page level.
var pagePipeline = async (context) => {
let pageBlocks = await logseq.Editor.getPageBlocksTree(context.page);
let extracts = [];

for (const block of pageBlocks) {

let result = await processBlock(block);
!!result && extracts.push(result);
}

extracts = extracts.flat();

//EXIT if no extracts found
if (!extracts || !extracts.length) {

logseq.App.showMsg('❗ Nothing to extract!');
return;
}

summarizeExtracts(extracts, pageBlocks[pageBlocks.length - 1]);
for (const block of pageBlocks) {
let result = await processBlock(block);
!!result && extracts.push(result);
}

extracts = extracts.flat();

//Extraction function which registers Extract as a context menu for a block
logseq.Editor.registerBlockContextMenuItem(
'Extract', blockPipeline
);
//EXIT if no extracts found
if (!extracts || !extracts.length) {
logseq.App.showMsg("❗ Nothing to extract!");
return;
}

//Extraction function which registers Extract as a context menu for a block
logseq.App.registerPageMenuItem(
'Extract', pagePipeline
);
// logseq.App.registerPageMenuItem(
// 'Extract', async (context) => {
summarizeExtracts(extracts, pageBlocks[pageBlocks.length - 1]);
};

// let pageBlocks = await logseq.Editor.getPageBlocksTree(context.page);
// pageBlocks.forEach((block) => processBlock(block));
// }
// );
//Extraction function which registers Extract as a context menu for a block
logseq.Editor.registerBlockContextMenuItem("Extract", blockPipeline);

//Extraction function which registers Extract as a context menu for a block
logseq.App.registerPageMenuItem("Extract", pagePipeline);
// logseq.App.registerPageMenuItem(
// 'Extract', async (context) => {

// let pageBlocks = await logseq.Editor.getPageBlocksTree(context.page);
// pageBlocks.forEach((block) => processBlock(block));
// }
// );
}

function getExtracts(currentBlock, regEx, extracts) {
//Get children of the current block
let children = currentBlock.children;

//Get children of the current block
let children = currentBlock.children;

//Find the extracts from the current block
let currentBlockExtracts = [...currentBlock.content.matchAll(regEx)];
//Find the extracts from the current block
let currentBlockExtracts = [...currentBlock.content.matchAll(regEx)];

//Create a map from current block's extracts
let currentBlockExtractsWithBlockRef = currentBlockExtracts.map((e) => { return { content: e[0], source: currentBlock }; });
//Create a map from current block's extracts
let currentBlockExtractsWithBlockRef = currentBlockExtracts.map((e) => {
return { content: e[0], source: currentBlock };
});

//Push the extracts map from current block into main extracts array
!!currentBlockExtracts.length && extracts.push(...currentBlockExtractsWithBlockRef);
//Push the extracts map from current block into main extracts array
!!currentBlockExtracts.length &&
extracts.push(...currentBlockExtractsWithBlockRef);

//If there are children then call this method recursively (filling the main extracts array which is passed as argument)
!!children.length && children.forEach((c) => getExtracts(c, regEx, extracts));
return;
//If there are children then call this method recursively (filling the main extracts array which is passed as argument)
!!children.length && children.forEach((c) => getExtracts(c, regEx, extracts));
return;
}

// bootstrap
logseq.ready(main).catch(console.error)
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-extract-plugin",
"version": "2.0.0",
"version": "2.0.3",
"description": "Extract highlighted/bold content from block or page",
"main": "index.html",
"author": "Sidharth Panwar",
Expand All @@ -10,7 +10,7 @@
"build": "parcel build --public-url . index.html"
},
"dependencies": {
"@logseq/libs": "^0.0.1-alpha.22"
"@logseq/libs": "^0.0.1-alpha.35"
},
"devDependencies": {
"parcel": "^2.0.0-beta.2",
Expand Down

0 comments on commit de81906

Please sign in to comment.