From ea68aff7ae432562c4157862ecc373a7d2ffdb2b Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Mon, 27 Nov 2023 09:10:12 -0800 Subject: [PATCH] feat: Doc formatting --- src/macrofier/engine.mjs | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 1a950378..543e9a57 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1271,11 +1271,6 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) return template } -function jsonToJsObject(json) { - json.replace(/\\"/g,"\uFFFF") - return json.replace(/"([^"]+)":/g, '$1:').replace(/\uFFFF/g, '\\\"') -} - function insertExampleMacros(template, examples, method, json, templates) { let content = '' @@ -1304,13 +1299,13 @@ function insertExampleMacros(template, examples, method, json, templates) { first = false const formatParams = (params, delimit, pretty = false) => params.map(p => JSON.stringify((example.json.params.find(x => x.name === p.name) || { value: null }).value, null, pretty ? ' ' : null)).join(delimit) - let hasObjectParam = example.json.params.find(p => typeof p.value === 'object') != null + let indent = ' '.repeat(json.info.title.length + method.name.length + 2) let params = formatParams(method.params, ', ') - if ((params.length > 80) || hasObjectParam) { + if (params.length + indent > 80) { params = formatParams(method.params, ',\n', true) params = params.split('\n') let first = params.shift() - params = params.map(p => ' ' + p) + params = params.map(p => indent + p) params.unshift(first) params = params.join('\n') } @@ -1323,7 +1318,7 @@ function insertExampleMacros(template, examples, method, json, templates) { .replace(/\$\{method\.result\.name\}/g, method.result.name) .replace(/\$\{method\.name\}/g, method.name) - .replace(/\$\{example\.params\}/g, jsonToJsObject(params)) + .replace(/\$\{example\.params\}/g, params) .replace(/\$\{example\.result\}/g, language.result) .replace(/\$\{example\.result\.item\}/g, Array.isArray(example.json.result.value) ? JSON.stringify(example.json.result.value[0], null, '\t') : '') .replace(/\$\{module\}/g, json.info.title) @@ -1331,16 +1326,12 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 2 - if (language.langcode === 'json') { - indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ - } + let indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ } const value = JSON.stringify(method.examples[index].params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - const objText = language.langcode === 'json' ? value : jsonToJsObject(value) - languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, objText) + languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, value) }) @@ -1349,16 +1340,12 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 2 - if (language.langcode === 'json') { - indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ - } + let indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ } const value = JSON.stringify(originalExample.params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - const objText = language.langcode === 'json' ? value : jsonToJsObject(value) - languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, objText) + languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, value) }) }