From 098ae594d28b74b0aaeef307689122d8e6a17889 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Wed, 14 Mar 2018 15:58:44 -0700 Subject: [PATCH] Add latest replaceVars code --- packages/api-explorer/src/lib/replace-vars.js | 103 +++++++++++------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/packages/api-explorer/src/lib/replace-vars.js b/packages/api-explorer/src/lib/replace-vars.js index fac245151..df2aaa73a 100644 --- a/packages/api-explorer/src/lib/replace-vars.js +++ b/packages/api-explorer/src/lib/replace-vars.js @@ -1,53 +1,78 @@ -// exports.replaceVars = function(out, variables) { +// const varReplacer = function (variables, defaults = [], glossaryTerms) { +// return (match, v) => { +// let isDefault = false; + +// // Handle glossary terms +// if (v.indexOf('glossary:') === 0) { +// const term = v.replace('glossary:', ''); +// const foundTerm = glossaryTerms.filter(gTerm => term === gTerm.term)[0]; +// if (foundTerm) { +// return ( +// ` +// +// ${foundTerm.term} +// +// +// - ${foundTerm.term} - +// ${foundTerm.definition} +// +// +// +// ` +// ); +// } +// } + +// // Don't break the <> syntax +// if (v.indexOf('keys:') === 0) { +// // tombstone('keysVariableFormat', '2-23-2018', { var: v }); +// v = v.split('keys:')[1]; +// } + +// let data = variables && variables[v] ? variables[v] : undefined; + +// const inKeys = variables && variables.keys && variables.keys[0][v]; +// const inVariables = variables && variables[v]; + +// // Use default +// if (!variables || !(inKeys || inVariables)) { +// isDefault = true; +// const userDefault = defaults.find(d => d.name === v); +// data = userDefault ? userDefault.default : v.toUpperCase(); +// } else if (inKeys) { +// // Keys array was passed in +// // We figure out keys in the frontend (so we have selectedApp) +// return ``; +// } + +// // Just a signle value +// return ``; +// }; +// }; + +// exports.replaceVars = function (variables, defaults, glossaryTerms, out) { // // If no variables (either from default or jwt, don't change anything) -// if (!variables) { +// if (!variables && !defaults) { // return out; // } -// + // // Variables will initially be string // if (typeof variables === 'string') { // try { // variables = JSON.parse(variables); // } catch (e) { // // TODO-JWT Try to figure out a way to deal with error -// console.log(e); +// return out; // } // } -// -// out = out.replace(/<<([-\w:]*?)>>/g, (match, v) => { -// const type = v.indexOf('keys:') >= 0 ? 'keys' : 'user'; -// -// if (type === 'keys') { -// const vName = v.split(':')[1]; -// if (variables.default) { -// for (const keyVar of variables.keys) { -// if (keyVar.name === vName) { -// return ``; -// } -// } -// } else if (variables.fromReadmeKey) { -// return ``; -// } -// -// // Directive handles replacing key variables and showing switcher ui -// return ``; -// } else if (variables.default) { -// for (const userVar of variables.user) { -// if (userVar.name === v) { -// return ``; -// } -// } -// -// // Fallback to uppercase if there is no default -// return ``; -// } else if (variables[v]) { -// return ``; -// } -// return ``; -// }); -// + +// const replacer = varReplacer(variables, defaults, glossaryTerms); + +// out = out.replace(/<<([-\w:]*?)>>/g, replacer); +// out = out.replace(/<<([-\w:]*?)>>/g, replacer); + // // Makes \<> display as <> in the frontend // out = out.replace(/\\<<([-\w:]*?)\\>>/g, (match, v) => `<<${v}>>`); -// + // return out; -// }; +// }