Last active
June 6, 2022 16:26
-
-
Save napcs/5fe1b90eab719cf73e071d69cd527f68 to your computer and use it in GitHub Desktop.
Quick Node script to generate Hugo static pages from TalentLMS API data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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