Skip to content

Instantly share code, notes, and snippets.

@napcs
Last active June 6, 2022 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save napcs/5fe1b90eab719cf73e071d69cd527f68 to your computer and use it in GitHub Desktop.
Save napcs/5fe1b90eab719cf73e071d69cd527f68 to your computer and use it in GitHub Desktop.
Quick Node script to generate Hugo static pages from TalentLMS API data
'use strict';
// The URL to your public catalog. Ensure you've enabled this public catalog so links work.
const URL = 'https://yourdomain.talentlms.com/catalog';
const fs = require('fs');
// Getting data from a local copy to ensure builds are stable.
let rawdata = fs.readFileSync('data/courses.json');
let courses = JSON.parse(rawdata);
for (let course of courses) {
let md = generateMarkdown(course)
let filename = course.name.replace(/[^a-z0-9]/gi, '_').toLowerCase() + ".md";
fs.writeFileSync(`content/courses/${filename}`, md);
}
// Generates the Markdown content with YAML frontmatter.
// Drafts are set based on whether a course is marked active.
// Course URL goes to the course ID in the public catalog.
function generateMarkdown(course) {
let draft = course.status !== "active";
let publicCourse = course.shared === 1;
let url = `${URL}/info/id:${course.id}`;
let str = `---
title: ${course.name}
draft: ${draft}
public: ${publicCourse}
courseUrl: ${url}
---
${course.description}
`;
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment