From 03ae1dd625dcf4a23b2c65df1b8a6a53e9de6356 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 14:37:54 +0100 Subject: [PATCH 01/10] Move settings to file --- src/gitbook.js | 14 +++----------- src/settings.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/settings.js diff --git a/src/gitbook.js b/src/gitbook.js index 70ae0528..e257eb08 100644 --- a/src/gitbook.js +++ b/src/gitbook.js @@ -5,17 +5,7 @@ const summarize = require('./gitbook/summary'); // const obsidianToMD = require('./gitbook/obsidian'); /* Load settings (if exist) */ -let settings = { - obsidianProject: '../obsidian', - gitbookProject: '../gitbook', -}; - -try { - settings = require('../settings.json'); - console.log('Settings loaded from settings.json'); -} catch { - console.log('Settings not found. Using default settings'); -} +const settings = require('settings.js'); /** @@ -35,6 +25,8 @@ async function main() { const index = await indexProject(settings.gitbookProject); // console.log(index) + /* ToDo: Remove unneccessary files and folders (.gitignore, .gitatributes, .git...) */ + /* Rename the files and folders */ const renamed = await renameProject(settings.gitbookProject, index); if (!renamed) { diff --git a/src/settings.js b/src/settings.js new file mode 100644 index 00000000..fe298eda --- /dev/null +++ b/src/settings.js @@ -0,0 +1,19 @@ +let settings = { + obsidianProject: '../obsidian', + gitbookProject: '../gitbook', +}; + +try { + const settingsFile = require('./settings.json'); + + settings = { + ...settings, + ...settingsFile, + }; + console.log('Custom settings loaded from settings.json'); +} catch { + console.log('Settings not found. Using default settings'); +} + +// export default settings; +module.exports = settings; From dada1f9285a464abece29311c95c2d428b6fbba5 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 14:44:52 +0100 Subject: [PATCH 02/10] fix settings syntax --- src/gitbook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gitbook.js b/src/gitbook.js index e257eb08..85ca8eb8 100644 --- a/src/gitbook.js +++ b/src/gitbook.js @@ -5,7 +5,7 @@ const summarize = require('./gitbook/summary'); // const obsidianToMD = require('./gitbook/obsidian'); /* Load settings (if exist) */ -const settings = require('settings.js'); +const settings = require('./settings.js'); /** From d08b6f65431359d34b38e4b60d937faed3cd5e93 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 15:33:42 +0100 Subject: [PATCH 03/10] Remove git paths in obsidian project --- src/gitbook/copy.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/gitbook/copy.js b/src/gitbook/copy.js index 81390cb5..f2acd6e9 100644 --- a/src/gitbook/copy.js +++ b/src/gitbook/copy.js @@ -16,6 +16,22 @@ async function copyProject(obsidian, gitbook) { await fse.remove(`${gitbook}/.obsidian`); console.log('> Removed Obsidian files'); + // Remove Git files + const gitPaths = ['.git', '.gitignore', '.gitattributes']; + + // gitPaths.forEach(async (path) => { + for (var i = 0; i < gitPaths.length; i++) { + const path = gitPaths[i]; + + const fullPath = `${gitbook}/${path}`; + const exists = await fse.pathExists(fullPath); + if (exists) { + await fse.remove(fullPath); + } + // }) + } + console.log('> Removed Git-related files'); + return true; } catch (e) { console.error('> Error copying Obsidian project to GitBook folder'); From d5dfc851b12a5213f2e0cad8826704e46862d50f Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 15:34:35 +0100 Subject: [PATCH 04/10] clean copy loop code --- src/gitbook/copy.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/gitbook/copy.js b/src/gitbook/copy.js index f2acd6e9..4110ec2f 100644 --- a/src/gitbook/copy.js +++ b/src/gitbook/copy.js @@ -19,16 +19,14 @@ async function copyProject(obsidian, gitbook) { // Remove Git files const gitPaths = ['.git', '.gitignore', '.gitattributes']; - // gitPaths.forEach(async (path) => { for (var i = 0; i < gitPaths.length; i++) { const path = gitPaths[i]; - + const fullPath = `${gitbook}/${path}`; const exists = await fse.pathExists(fullPath); if (exists) { await fse.remove(fullPath); } - // }) } console.log('> Removed Git-related files'); From 768df73941c189d21926d769defb9c290f6a6e16 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 17:26:14 +0100 Subject: [PATCH 05/10] Initial link replace version --- README.md | 2 + src/gitbook.js | 6 +- src/obsidian-md/parse-links.js | 157 +++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/obsidian-md/parse-links.js diff --git a/README.md b/README.md index 428d60ac..6f9d2db1 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,9 @@ As soon as you push code to your Obsidian repository, it will trigget an action ## Status and known issues: - [x] Transform Obsidian project in Gitbook project (naming and structure) - [x] Generate a valid SUMMARY.md +- [x] Remove git related files and folders - [ ] Transform page links to plain markdown links (next-up) +- [ ] Replace tildes and ñ instead of removing them (next-up) - [ ] Show all the pages linking to the current one (on the roadmap) - [ ] Link to blocks (on the roadmap) diff --git a/src/gitbook.js b/src/gitbook.js index 85ca8eb8..8dc0f0b8 100644 --- a/src/gitbook.js +++ b/src/gitbook.js @@ -2,7 +2,8 @@ const copyProject = require('./gitbook/copy'); const indexProject = require('./gitbook/index-project'); const renameProject = require('./gitbook/rename'); const summarize = require('./gitbook/summary'); -// const obsidianToMD = require('./gitbook/obsidian'); + +const parseLinks = require('./obsidian-md/parse-links'); /* Load settings (if exist) */ const settings = require('./settings.js'); @@ -48,6 +49,9 @@ async function main() { /* Convert the Obsidian syntax to markdown */ // obsidianToMd(index); + /* Parse page links */ + const pageIndex = parseLinks(settings.gitbookProject, index); + return true } diff --git a/src/obsidian-md/parse-links.js b/src/obsidian-md/parse-links.js new file mode 100644 index 00000000..b8b38977 --- /dev/null +++ b/src/obsidian-md/parse-links.js @@ -0,0 +1,157 @@ +const fs = require('fs'); +const fse = require('fs-extra'); + +const indexProject = require('../gitbook/index-project'); + +// Page links map +const pageLinks = {}; + +/** + * Get page name from original filename + * + * @param filename {String} Filename to turn into page name + * @return {String} Page name (without stuff like extensions) + */ +function pageName(filename) { + return filename.replace('.md', ''); +} + +/** + * Search for page link in node index + * + * @param page {String} Page name to find link + * @param index {Object} Project index to find into + * @return {String} Path to the page + */ +function findPageLink(page, index) { + const nodes = Object.keys(index); + + // ToDo: Is there a / in page name? + + for (let i = 0; i < nodes.length; i += 1) { + const node = index[nodes[i]]; + + // Deep search in folders + if (typeof node.content !== 'undefined') { + const found = findPageLink(page, node.content); + + if (found) { + return `${nodes[i]}/${found}`; + } + } + + // Check page name + if (pageName(node.name) === page) { + return nodes[i]; + } + } + + return false; +} + +/** + * Get a page link + * + * @param page {String} Page name to find link + * @param index {Object} Project index to find into + * @return {String} Path to the page + */ +function pageLink(page, index) { + if (pageLinks[page]) { + return pageLinks[page]; + } + + const link = findPageLink(page, index); + pageLinks[page] = link; + + return link; +} + +/** + * Parse page links in a file + */ +async function parsePageLinks(filename, index) { + try { + // If is not a markdown file, jump to the next file + if (filename.indexOf('.md') === -1) { + // console.log('File is not markdown. Skipping.'); + return false; + } + + // If file dont exist, jump to the next file + const exists = await fse.pathExists(filename); + if (!exists) { + return false; + } + + // Get page content + let pageText = fs.readFileSync(filename, 'utf8'); + + // If not content, jump. It might be an empty page. + + // Regex: Find "[[ SOMETHING ]]" (without quotes) + const pageLinkRegex = /(\[\[)(\s*\b)([^\[\]]*)(\b\s*)(\]\])/g; + const links = pageText.match(pageLinkRegex); + const uniqueLinks = [...new Set(links)]; + + // If links, replace them... + for (let i = 0; i < uniqueLinks.length; i += 1) { + const pageName = uniqueLinks[i].replace('[[', '').replace(']]', ''); + const pageLinkPath = pageLink(pageName, index); + const mdlink = `[${pageName}](${pageLinkPath})` + + // Unable to use .replace() with RegEx because of [] chars + pageText = pageText.split(uniqueLinks[i]).join(mdlink); + } + + // ...And store the new file + fs.writeFileSync(filename, pageText); + + } catch (err) { + console.error(err) + } +} + +/** + * Parse links in all pages for a given index + * + * @param index {Object} Project index to find into + */ +async function parseLinks(folder, index, rootIndex) { + let newIndex = { ...index }; + let originalIndex = { ...rootIndex } + + if (!index) { + newIndex = indexProject(folder); + } + if (!rootIndex) { + originalIndex = newIndex; + } + + try { + const nodes = Object.keys(newIndex); + + for (let i = 0; i < nodes.length; i += 1) { + const node = newIndex[nodes[i]]; + + // If is folder, go recursive + if (typeof node.content !== 'undefined') { + await parseLinks(`${folder}/${nodes[i]}`, node.content, originalIndex); + } + + // Parse page links + console.log(`~~> ${folder}/${nodes[i]}`) + await parsePageLinks(`${folder}/${nodes[i]}`, originalIndex); + } + + console.log('> First level page links created!'); + + return pageLinks; + } catch (e) { + console.error('Error: Unable to rewrite Obsidian page links'); + console.error(e); + return null; + } +} + +module.exports = parseLinks; From 7b334e5820e495301cd90cfdb50ccff80946ae22 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 17:41:22 +0100 Subject: [PATCH 06/10] Update gitbook links --- src/obsidian-md/parse-links.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/obsidian-md/parse-links.js b/src/obsidian-md/parse-links.js index b8b38977..738a6694 100644 --- a/src/obsidian-md/parse-links.js +++ b/src/obsidian-md/parse-links.js @@ -98,10 +98,12 @@ async function parsePageLinks(filename, index) { for (let i = 0; i < uniqueLinks.length; i += 1) { const pageName = uniqueLinks[i].replace('[[', '').replace(']]', ''); const pageLinkPath = pageLink(pageName, index); - const mdlink = `[${pageName}](${pageLinkPath})` + + // In GitBook, relative links to the book are preceded by ../ + const gitbookLink = `[${pageName}](../${pageLinkPath})` // Unable to use .replace() with RegEx because of [] chars - pageText = pageText.split(uniqueLinks[i]).join(mdlink); + pageText = pageText.split(uniqueLinks[i]).join(gitbookLink); } // ...And store the new file From 2360d0d8b6a2c7e61fcc33821f29de4c2a61ac9a Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 17:43:37 +0100 Subject: [PATCH 07/10] Add github folder to git removable list --- src/gitbook/copy.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gitbook/copy.js b/src/gitbook/copy.js index 4110ec2f..4e097b0e 100644 --- a/src/gitbook/copy.js +++ b/src/gitbook/copy.js @@ -17,7 +17,12 @@ async function copyProject(obsidian, gitbook) { console.log('> Removed Obsidian files'); // Remove Git files - const gitPaths = ['.git', '.gitignore', '.gitattributes']; + const gitPaths = [ + '.git', + '.gitignore', + '.gitattributes', + '.github', + ]; for (var i = 0; i < gitPaths.length; i++) { const path = gitPaths[i]; From 37a0e80d16007c2d0d5ebb1c55ed961d8d15ddbd Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 18:01:59 +0100 Subject: [PATCH 08/10] try different depths --- src/obsidian-md/parse-links.js | 35 +++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/obsidian-md/parse-links.js b/src/obsidian-md/parse-links.js index 738a6694..141afa6e 100644 --- a/src/obsidian-md/parse-links.js +++ b/src/obsidian-md/parse-links.js @@ -99,8 +99,37 @@ async function parsePageLinks(filename, index) { const pageName = uniqueLinks[i].replace('[[', '').replace(']]', ''); const pageLinkPath = pageLink(pageName, index); + // ToDo: If not link found, act in consequence + if (!pageLinkPath) { + console.warn(`[warn] Unable to find link for ${pageName}`); + break; + } + // In GitBook, relative links to the book are preceded by ../ - const gitbookLink = `[${pageName}](../${pageLinkPath})` + /* + ToDo: + In nested folders, internal links are not working. + It might be related to the ../ at the begining. + Maybe trying to use as may ../ as levels are in the path? + -> (check src/gitbook/summary.md). + */ + const depth = pageLinkPath.split('/').length - 1; + + // Initial depth of '0' + let pageDepth = './'; + + if (depth > 0) { + // Reset page depth + pageDepth = ''; + + // And add the nested levels required + for (let j = 0; j < depth; j += 1) { + pageDepth += '../'; + } + } + + const gitbookLink = `[${pageName}](${pageDepth}${pageLinkPath})` + // console.log(pageDepth, pageLinkPath); // Unable to use .replace() with RegEx because of [] chars pageText = pageText.split(uniqueLinks[i]).join(gitbookLink); @@ -142,11 +171,11 @@ async function parseLinks(folder, index, rootIndex) { } // Parse page links - console.log(`~~> ${folder}/${nodes[i]}`) + // console.log(`~~> ${folder}/${nodes[i]}`) await parsePageLinks(`${folder}/${nodes[i]}`, originalIndex); } - console.log('> First level page links created!'); + // console.log('> First level page links created!'); return pageLinks; } catch (e) { From 1090cbed85c74efd9e34171cdebd84a5336fa943 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 18:11:51 +0100 Subject: [PATCH 09/10] use file depth instead of link depth --- src/obsidian-md/parse-links.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/obsidian-md/parse-links.js b/src/obsidian-md/parse-links.js index 141afa6e..7674f2cb 100644 --- a/src/obsidian-md/parse-links.js +++ b/src/obsidian-md/parse-links.js @@ -113,7 +113,10 @@ async function parsePageLinks(filename, index) { Maybe trying to use as may ../ as levels are in the path? -> (check src/gitbook/summary.md). */ - const depth = pageLinkPath.split('/').length - 1; + + // ToDo: Make this dynamic based on the orifinal fonder parsed + const originDepth = '../gitbook/'.split('/').length; + const depth = (filename.split('/').length - 1) - originDepth; // Initial depth of '0' let pageDepth = './'; @@ -129,7 +132,7 @@ async function parsePageLinks(filename, index) { } const gitbookLink = `[${pageName}](${pageDepth}${pageLinkPath})` - // console.log(pageDepth, pageLinkPath); + // console.log(filename, pageDepth, pageLinkPath); // Unable to use .replace() with RegEx because of [] chars pageText = pageText.split(uniqueLinks[i]).join(gitbookLink); From b9cd177676e0568fe21bb951c8c2218c281ff493 Mon Sep 17 00:00:00 2001 From: CodingCarlos Date: Wed, 12 Jan 2022 18:18:23 +0100 Subject: [PATCH 10/10] fix depth in links --- src/obsidian-md/parse-links.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/obsidian-md/parse-links.js b/src/obsidian-md/parse-links.js index 7674f2cb..8898c0e1 100644 --- a/src/obsidian-md/parse-links.js +++ b/src/obsidian-md/parse-links.js @@ -113,10 +113,10 @@ async function parsePageLinks(filename, index) { Maybe trying to use as may ../ as levels are in the path? -> (check src/gitbook/summary.md). */ - + // ToDo: Make this dynamic based on the orifinal fonder parsed const originDepth = '../gitbook/'.split('/').length; - const depth = (filename.split('/').length - 1) - originDepth; + const depth = filename.split('/').length - originDepth; // Initial depth of '0' let pageDepth = './'; @@ -133,6 +133,7 @@ async function parsePageLinks(filename, index) { const gitbookLink = `[${pageName}](${pageDepth}${pageLinkPath})` // console.log(filename, pageDepth, pageLinkPath); + console.log(filename, depth, pageDepth); // Unable to use .replace() with RegEx because of [] chars pageText = pageText.split(uniqueLinks[i]).join(gitbookLink);