From 35bfdcbe4c4d3ebf69ff2fb8b6c11abf3125c6ca Mon Sep 17 00:00:00 2001 From: Janik von Rotz Date: Fri, 2 Jun 2023 17:00:13 +0200 Subject: [PATCH] init svg conversion --- S3.svg | 12 +++++++++ build.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 S3.svg diff --git a/S3.svg b/S3.svg new file mode 100644 index 00000000..71903cee --- /dev/null +++ b/S3.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/build.js b/build.js index f16a571c..99f3f986 100644 --- a/build.js +++ b/build.js @@ -4,6 +4,7 @@ var path = require('path') /* arguments: all index +canvas convert assets sitemap @@ -44,7 +45,7 @@ function sanitizeAssetname(file) { } function loopMdFiles() { - return fs.readdirSync(__dirname).filter(file => (file.slice(-3) === '.md') && (ignoreFiles.indexOf(file) != 0)) + return fs.readdirSync(__dirname).filter(file => (path.extname(file) === '.md') && (ignoreFiles.indexOf(file) != 0)) } const groupBy = key => array => @@ -54,6 +55,58 @@ const groupBy = key => array => return objectsByKeyValue; }, {}); +function renderRect(node) { + return `\t\n` +} + +function renderArrow(node) { + return `\t\n` +} + +function convertCanvasToSVG(content) { + nodes = content['nodes'] + edges = content['edges'] + + let svg = "" + svg += '\n' + svg += '\n' + svg += '\n' + + for (const node of nodes) { + svg += renderRect(node) + } + + for (const edge of edges) { + fromNode = nodes.filter(node => (node['id'] === edge['fromNode'])) + toNode = nodes.filter(node => (node['id'] === edge['toNode'])) + let fromX, fromY, toX, toY = 0 + + // Calculate x and y position for from and to point + if (edge['fromSide'] === 'right') { + fromX = fromNode['x'] + fromNode['width'] + fromY = fromNode['y'] + fromNode['height'] / 2 + + if (edge['fromSide'] === 'bottom') { + fromX = fromNode['x'] + fromNode['with'] / 2 + fromY = fromNode['y'] + } + if (edge['fromSide'] === 'left') { + fromX = fromNode['x'] + fromY = fromNode['y'] + fromNode['height'] / 2 + + } + if (edge['fromSide'] === 'top') { + fromX = fromNode['x'] + fromNode['width'] / 2 + fromY = fromNode['y'] + fromNode['height'] + } + svg += renderArrow(edge) + } + + svg += '' + + return svg +} + function convert(content, file) { // Convert Obsidian cancas @@ -153,6 +206,30 @@ if (!firstArg || ['all', 'index'].indexOf(firstArg) >= 0) { console.log('Building title index finished.') } + +if (!firstArg || ['all', 'canvas'].indexOf(firstArg) > 0) { + + // log + console.log('Convert canvas files ...') + + // Loop all cavas files + fs.readdirSync(__dirname).filter(file => (path.extname(file) === '.canvas')).forEach((file) => { + + // Get file name + let fileName = file.replace('\.canvas', '') + + // Get content + let content = fs.readFileSync(file, 'utf8') + + content = convertCanvasToSVG(JSON.parse(content)) + + fs.writeFileSync(fileName + '.svg', content) + }) + + // log + console.log('Converting canvas files finished.') +} + if (!firstArg || ['all', 'convert'].indexOf(firstArg) > 0) { // log