From 2ae78fb4eae856671e020d48074eb86f1449de13 Mon Sep 17 00:00:00 2001 From: "Tylah Kapa [SSW]" Date: Mon, 5 Aug 2024 11:55:47 +1000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Publish=20profile=20data=20as=20a?= =?UTF-8?q?=20.md=20file=20(#657)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add gray-matter to convert to Markdown * Update Gatsby node to create profile md * Sanitise markdown * Update with fullname --- gatsby-node.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + yarn.lock | 3 ++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/gatsby-node.js b/gatsby-node.js index da1f6b9..c1f394d 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -8,6 +8,7 @@ const { getViewDataFromCRM, getUsersSkills } = require('./src/helpers/CRMApi'); const appInsights = require('applicationinsights'); const fs = require('fs'); const siteconfig = require('./site-config'); +const matter = require('gray-matter'); const environment = process.env.NODE_ENV; const appInsightsConnectionString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING; @@ -287,6 +288,7 @@ exports.createPages = async function ({ actions, graphql }) { jobTitle } html + rawMarkdownBody } } peopleCRM: allCrmDataCollection { @@ -431,9 +433,22 @@ exports.createPages = async function ({ actions, graphql }) { (x) => x.name === node.parent.name.replace(profileChineseTag, '') ), html: node.html, + rawMarkdown: node.rawMarkdownBody, }; }); + const peoplePath = './public'; + + if (!fs.existsSync(peoplePath)) { + fs.mkdirSync(peoplePath); + } + + const peopleList = people + .filter((person) => !person.path.includes('alumni')) + .map((person) => person.path); + + fs.writeFileSync(`${peoplePath}/people.json`, JSON.stringify(peopleList)); + people.forEach((person) => { /* eslint-disable no-console */ console.log('Creating page for ' + person.slug); @@ -473,5 +488,54 @@ exports.createPages = async function ({ actions, graphql }) { isPermanent: true, }); } + + if (person.path.includes('alumni')) { + return; + } + + const filePath = `./public/${person.path}`; + if (!fs.existsSync(filePath)) { + fs.mkdirSync(filePath); + } + + const skills = []; + skills.push( + ...person.dataCRM.skills.intermediateSkills.map((skill) => skill.service) + ); + skills.push( + ...person.dataCRM.skills.advancedSkills.map((skill) => skill.service) + ); + + const sanitisedMarkdown = (input) => { + const lines = input.split('\n'); + + const filteredLines = lines.filter(x => { + const imgRegex = /!\[.*\](.*)/; + return ( + !imgRegex.test(x) && + !x.trim().includes('[[imgBadge]]') && + !x.trim().includes('[Editing profiles]') && + !x.trim().includes('
') && + x.length !== 0 + ); + }); + + var output = filteredLines.join('\n') + return output.split('\n', 2).join('\n'); + } + + var profileData = { + skills: skills.join(' | '), + presenter: { + name: person.dataCRM.fullName, + peopleProfileURL: 'https://ssw.com.au/people/' + person.path, + }, + about: sanitisedMarkdown(person.rawMarkdown), + }; + + fs.writeFileSync( + `${filePath}/profile.md`, + matter.stringify('', profileData) + ); }); }; diff --git a/package.json b/package.json index 7c3442d..53a00dc 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "gatsby-transformer-json": "^5.13.1", "gatsby-transformer-remark": "^3.2.0", "gatsby-transformer-sharp": "^5.3.1", + "gray-matter": "^4.0.3", "history": "^5.3.0", "https-browserify": "^1.0.0", "lottie-web": "^5.12.2", diff --git a/yarn.lock b/yarn.lock index 6c46969..233db04 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13755,7 +13755,7 @@ __metadata: languageName: node linkType: hard -"gray-matter@npm:^4.0.2": +"gray-matter@npm:^4.0.2, gray-matter@npm:^4.0.3": version: 4.0.3 resolution: "gray-matter@npm:4.0.3" dependencies: @@ -23488,6 +23488,7 @@ __metadata: gatsby-transformer-json: "npm:^5.13.1" gatsby-transformer-remark: "npm:^3.2.0" gatsby-transformer-sharp: "npm:^5.3.1" + gray-matter: "npm:^4.0.3" history: "npm:^5.3.0" https-browserify: "npm:^1.0.0" lighthouse: "npm:^9.6.8"