Skip to content

Commit

Permalink
✨ Publish profile data as a .md file (#657)
Browse files Browse the repository at this point in the history
* Add gray-matter to convert to Markdown

* Update Gatsby node to create profile md

* Sanitise markdown

* Update with fullname
  • Loading branch information
tkapa authored Aug 5, 2024
1 parent d892815 commit 2ae78fb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
64 changes: 64 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -287,6 +288,7 @@ exports.createPages = async function ({ actions, graphql }) {
jobTitle
}
html
rawMarkdownBody
}
}
peopleCRM: allCrmDataCollection {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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('<br/>') &&
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)
);
});
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2ae78fb

Please sign in to comment.