From 716927681898fa154487ffe962f629d7f9483e1b Mon Sep 17 00:00:00 2001 From: ikoamu Date: Fri, 14 Jun 2024 19:40:22 +0900 Subject: Create local shell --- .gitignore | 2 + action.yml | 2 +- generate_graphdata.js | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++ generate_json.js | 114 ------------------------------------------------- local.sh | 31 ++++++++++++++ package.json | 2 +- 6 files changed, 150 insertions(+), 116 deletions(-) create mode 100644 generate_graphdata.js delete mode 100644 generate_json.js create mode 100755 local.sh diff --git a/.gitignore b/.gitignore index d22afe2..b63e158 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ DS_Store graphdata.json searchdata.json notes/ +org-roam-ui/ +out/ \ No newline at end of file diff --git a/action.yml b/action.yml index 36778ea..af51b3b 100644 --- a/action.yml +++ b/action.yml @@ -58,7 +58,7 @@ runs: shell: bash - name: Generate graphdata.json working-directory: action - run: npm run generate --script_params='../orgs/${{ inputs.org-roam-directory }}/${{ inputs.org-roam-db-filename }}' + run: npm run generate:graphdata --script_params='../orgs/${{ inputs.org-roam-directory }}/${{ inputs.org-roam-db-filename }}' shell: bash - name: Create Notes working-directory: action diff --git a/generate_graphdata.js b/generate_graphdata.js new file mode 100644 index 0000000..0a627a8 --- /dev/null +++ b/generate_graphdata.js @@ -0,0 +1,115 @@ +console.log("db file", process.argv[2]); + +const sqlite3 = require('sqlite3').verbose(); +const dbFile = process.argv[2]; +const db = new sqlite3.Database(dbFile); +const fs = require('fs'); + +/** + * Parses the string of properties and returns an object containing the key-value pairs. + * If the key is 'FILE', the corresponding value is processed to extract the filename. + * @param {string} s - The string of properties. + * @returns {Object} - The object containing the parsed properties. + */ +function parseProperties(s) { + const properties = {}; + s.match(/\"(.*?)\" \. \"(.*?)\"/g).forEach(match => { + const [key, value] = match.match(/\"(.*?)\"/g).map(v => v.replace(/"/g, '')); + properties[key] = key === 'FILE' ? getFilename(value) : value; + }); + return removeQuotesFromObject(properties); +} + +/** + * Removes the double quotes from the object. + * @param {Object} obj - The object to be processed. + * @returns {Object} - The object without double quotes. + */ +function removeQuotesFromObject(obj) { + for (let key in obj) { + if (typeof obj[key] === 'string') { + obj[key] = obj[key].replace(/^"|"$/g, ''); + } else if (typeof obj[key] === 'object') { + obj[key] = removeQuotesFromObject(obj[key]); + } + } + return obj; +} + +/** + * Extracts the filename from the path. + * @param {string} path - The path to be processed. + * @returns {string} - The filename. + */ +function getFilename(path) { + return path.split('/').pop(); +} + +const queryNodes = ` +SELECT + GROUP_CONCAT(tags.tag) AS tags, + nodes.properties, + nodes.olp, + nodes.pos, + nodes.level, + nodes.title, + nodes.file, + nodes.id +FROM + nodes +LEFT JOIN + tags +ON + nodes.id = tags.node_id +GROUP BY + nodes.id +`; + +const queryLinks = ` +SELECT + type, dest as target, source +FROM + links +WHERE + type = '"id"' +`; + +const queryTags = ` +SELECT + tags.tag +FROM + tags +GROUP BY + tags.tag +`; + +const graphdata = { + type: "graphdata", + data: {}, +}; +db.all(queryNodes, (_, nodes) => + db.all(queryLinks, (_, links) => + db.all(queryTags, (_, tags) => { + graphdata.data.nodes = nodes.map(node => { + const splitTags = node.tags?.split(',') ?? [null]; + const olp = node.olp?.replace(/\(\"|\"\)/g, '').split(' ') ?? null; + return{ + ...node, + olp, + tags: splitTags, + file: getFilename(node.file), + properties: parseProperties(node.properties), + }; + }); + graphdata.data.links = links; + graphdata.data.tags = tags.length ? tags.map(t => t.tag) : null; + + fs.writeFile('graphdata.json', JSON.stringify(removeQuotesFromObject(graphdata)), (err) => { + if (err) throw err; + console.log('The file has been saved!'); + }); + }), + ), +); + +db.close(); diff --git a/generate_json.js b/generate_json.js deleted file mode 100644 index 9649443..0000000 --- a/generate_json.js +++ /dev/null @@ -1,114 +0,0 @@ -console.log("db file", process.argv[2]); -const sqlite3 = require('sqlite3').verbose(); -const dbFile = process.argv[2]; -const db = new sqlite3.Database(dbFile); -const fs = require('fs'); - -/** - * Parses the string of properties and returns an object containing the key-value pairs. - * If the key is 'FILE', the corresponding value is processed to extract the filename. - * @param {string} s - The string of properties. - * @returns {Object} - The object containing the parsed properties. - */ -function parseProperties(s) { - const properties = {}; - s.match(/\"(.*?)\" \. \"(.*?)\"/g).forEach(match => { - const [key, value] = match.match(/\"(.*?)\"/g).map(v => v.replace(/"/g, '')); - properties[key] = key === 'FILE' ? getFilename(value) : value; - }); - return removeQuotesFromObject(properties); -} - -/** - * Removes the double quotes from the object. - * @param {Object} obj - The object to be processed. - * @returns {Object} - The object without double quotes. - */ -function removeQuotesFromObject(obj) { - for (let key in obj) { - if (typeof obj[key] === 'string') { - obj[key] = obj[key].replace(/^"|"$/g, ''); - } else if (typeof obj[key] === 'object') { - obj[key] = removeQuotesFromObject(obj[key]); - } - } - return obj; -} - -/** - * Extracts the filename from the path. - * @param {string} path - The path to be processed. - * @returns {string} - The filename. - */ -function getFilename(path) { - return path.split('/').pop(); -} - -const queryNodes = ` -SELECT - GROUP_CONCAT(tags.tag) AS tags, - nodes.properties, - nodes.olp, - nodes.pos, - nodes.level, - nodes.title, - nodes.file, - nodes.id -FROM - nodes -LEFT JOIN - tags -ON - nodes.id = tags.node_id -GROUP BY - nodes.id -`; - -const queryLinks = ` -SELECT - type, dest as target, source -FROM - links -WHERE - type = '"id"' -`; - -const queryTags = ` -SELECT - tags.tag -FROM - tags -GROUP BY - tags.tag -`; - -const graphdata = { - type: "graphdata", - data: {}, -}; -db.all(queryNodes, (_, nodes) => - db.all(queryLinks, (_, links) => - db.all(queryTags, (_, tags) => { - graphdata.data.nodes = nodes.map(node => { - const splitTags = node.tags?.split(',') ?? [null]; - const olp = node.olp?.replace(/\(\"|\"\)/g, '').split(' ') ?? null; - return{ - ...node, - olp, - tags: splitTags, - file: getFilename(node.file), - properties: parseProperties(node.properties), - }; - }); - graphdata.data.links = links; - graphdata.data.tags = tags.length ? tags.map(t => t.tag) : null; - - fs.writeFile('graphdata.json', JSON.stringify(removeQuotesFromObject(graphdata)), (err) => { - if (err) throw err; - console.log('The file has been saved!'); - }); - }), - ), -); - -db.close(); diff --git a/local.sh b/local.sh new file mode 100755 index 0000000..c27632b --- /dev/null +++ b/local.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +read -p "Enter the ROAM_PATH: " ROAM_PATH +read -p "Enter the ROAM_DB_FILE: " ROAM_DB_FILE +ROAM_DB_PATH=$ROAM_PATH/$ROAM_DB_FILE +ROAM_IMG_PATH=$ROAM_PATH/img + +# If org-roam-ui directory does not exists, clone the org-roam-ui repository. +if [ ! -d "org-roam-ui" ]; then + git clone -b publish-org-roam-ui https://github.com/ikoamu/org-roam-ui +fi + +# Generate data for org-roam-ui +npm install +npm run generate:graphdata --script_params=$ROAM_DB_PATH +./create_notes.sh $ROAM_PATH +npm run generate:search + +# Copy files to the org-roam-ui directory +cp -f searchdata.json org-roam-ui/components/Search/ +if [ -d $ROAM_IMG_PATH ]; then + cp -r $ROAM_IMG_PATH org-roam-ui/public +fi + +pushd org-roam-ui +yarn install +pushd standalone +./build-standalone-server.sh ../.. +mv out ../../ +popd +popd \ No newline at end of file diff --git a/package.json b/package.json index 53796a3..44e9bf3 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "1.0.0", "description": "", "scripts": { - "generate": "node ./generate_json.js $npm_config_script_params", + "generate:graphdata": "node ./generate_graphdata.js $npm_config_script_params", "generate:search": "node ./generate_searchdata.mjs" }, "keywords": [], -- cgit