Skip to content

Commit

Permalink
Update utils for new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ashley-hebler committed Sep 14, 2020
1 parent cadfbf1 commit 9221628
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 81 additions & 0 deletions docs/config/tasks/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
const fs = require('fs-extra');
const nunjucks = require('nunjucks');
const {
docsStyles,
siteURL,
mappedGithubData,
} = require('../../config/paths.js');

const generateClassName = (str) => {
let className = str;
const parens = /\(([^)]+)\)/;
const parensMatch = parens.exec(str);
if (parensMatch && typeof parensMatch[1] !== 'undefined') {
[, className] = parensMatch;
}
return className;
};

const generateName = (str) => {
let extractedName = str;
const parens = /\(([^)]+)\)/;
const parensMatch = parens.exec(str);
if (parensMatch && typeof parensMatch[1] !== 'undefined') {
extractedName = extractedName.replace(parens, '').trim();
}
return extractedName;
};

const generateTemplate = async (str) => {
const isFile = str.includes('.html');
let template = str;
if (isFile) {
const codePath = `${docsStyles}${str}`;
template = fs.readFileSync(codePath, 'utf-8');
}
return template;
}

const renderTemplate = async (template, data) => {
const env = nunjucks.configure('./assets/scss');
let rendered = '';
try {
rendered = env.renderString(template, { ...data, siteURL });
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
}
return rendered
}

// If string is .classname, make it just classname
const stripSelector = (str) => {
return str[0] === '.' ? str.substring(1) : str
}


const readUsageInfo = async () => {
let github = {};
try {
github = await fs.readJson(mappedGithubData.out);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
}
return github;
};

const findUsageInfo = (usageInfo, className) => {
return typeof usageInfo.classData[stripSelector(className)] !==
'undefined'
? usageInfo.classData[stripSelector(className)].searchDataArr
: [];
}

const slugify = text => {
return text
.toString()
Expand All @@ -14,6 +88,13 @@ const stripTags = str => {
};

module.exports = {
findUsageInfo,
generateClassName,
generateName,
generateTemplate,
readUsageInfo,
renderTemplate,
slugify,
stripSelector,
stripTags,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dev": "SITE_ENV=development node docs/config/tasks/build.js && SITE_ENV=development eleventy --serve",
"test": "npm run lint && npm run build",
"release": "np --any-branch",
"type": "tsc ./docs/config/ts/hello.ts"
"type": "tsc ./docs/config/ts/hello.ts && node ./docs/config/ts/hello.js"
},
"files": [
"/assets"
Expand Down

0 comments on commit 9221628

Please sign in to comment.