Skip to content

Commit

Permalink
Index Node Essentials link shortcodes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lumenwrites committed Jun 3, 2023
1 parent a3f4081 commit 1a29bd2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions godot-node-essentials
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dotenv": "^16.0.3",
"fs-extra": "^11.1.1",
"gray-matter": "^4.0.3",
"slugify": "^1.6.6",
"zip-a-folder": "^1.1.5"
}
}
47 changes: 40 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ dotenv.config()
import fs from 'fs-extra'
import path from 'path'
import matter from 'gray-matter'
import { copyFiles, readText, saveText, ensureDirExists, readJson, saveJson } from './utils'
import { copyFiles, readText, saveText, ensureDirExists, readJson, saveJson, slugify } from './utils'
import { zip } from 'zip-a-folder'

const WORKING_DIR = process.cwd() // + '/course-content' // '/learn-to-code-with-godot' // + '/course-content' // + '/godot-node-essentials' // + `/learn-to-code-from-zero-test`
const WORKING_DIR = process.cwd() + '/godot-node-essentials' // + '/course-content' // '/learn-to-code-with-godot' // + '/course-content' // + '/godot-node-essentials' // + `/learn-to-code-from-zero-test`
const CONTENT_DIR = `${WORKING_DIR}/content-gdschool`
const OUTPUT_DIR = `${WORKING_DIR}/content-gdschool-processed`
const RELEASES_DIR = `${WORKING_DIR}/content-gdschool-releases`
Expand All @@ -21,7 +21,7 @@ async function main() {
copyFiles(CONTENT_DIR, OUTPUT_DIR)
// Find all code files in Godot project folders, so that I can later use them to replace include shortcodes inside codeblocks
const codeFiles = indexCodeFiles()

const lessonFiles = indexLessonFiles() // needed to create links with shortcodes like {{ link lesson-slug subheading }}
// Process the content of the landing page
courseIndexText = rewriteImagePaths(courseIndexText, `/courses/${courseFrontmatter.slug}`)
saveText(`${OUTPUT_DIR}/_index.md`, courseIndexText)
Expand Down Expand Up @@ -50,7 +50,7 @@ async function main() {
lessonText = processCodeblocks(lessonText, lessonFileName, codeFiles)

// let lessonUrl = `/course/${courseFrontmatter.slug}/${sectionFrontmatter.slug}/${lessonFrontmatter.slug}`
lessonText = rewriteLinks(lessonText, `/course/${courseFrontmatter.slug}`)
lessonText = rewriteLinks(lessonText, `/course/${courseFrontmatter.slug}`, lessonFiles)

// Saving the processed lesson, in place.
saveText(lessonFilePath, lessonText)
Expand All @@ -74,13 +74,19 @@ function parseConfig(config) {
}

//
function rewriteLinks(lessonText, courseUrl) {
function rewriteLinks(lessonText, courseUrl, lessonFiles) {
// TODO - some links have anchor tags linking to subheadings, like {{ link Lesson subheading }}
// const linkRegex = /{{\s*link\s+([^\s{}]+)\s*}}/g
const linkRegex = /{{\s*link\s+([\w-]+)\s*([\w-]*)\s*}}/g

// In the future, we should have shortcodes like {{ link lesson-slug subheading }}
// Then, we'd replace such shortcodes with links like this: [Lesson Title](/course/section-slug/lesson-slug#subheading)
// But, the way Node Essentails course is written, the shortcodes are like this: {{ link LessonFileName subheading }}
lessonText = lessonText.replace(linkRegex, (match, fileName, headingSlug) => {
const modifiedLink = `[${fileName}](${courseUrl}/${fileName}/${fileName})}`
const lesson = lessonFiles.find((lesson) => lesson.fileName === fileName)
let fullPath = `${courseUrl}/${lesson.sectionSlug}/${lesson.slug}`
if (headingSlug) fullPath += `#${headingSlug}`
const modifiedLink = `[${fileName}](${fullPath})`
return modifiedLink
})
return lessonText
Expand Down Expand Up @@ -220,7 +226,6 @@ function indexCodeFiles() {
// const folderName = currentPath.split('/').at(-1)
// if (config.ignoreDirs && config.ignoreDirs.includes(folderName)) return
if (['.gd', '.shader'].includes(fileExt)) {
console.log('YES')
if (['.shader'].includes(fileExt)) console.log('Found shader', fileName)
// console.log(godotProjectFolder, filePath);
codeFiles.push({
Expand All @@ -236,6 +241,34 @@ function indexCodeFiles() {
return codeFiles
}

// To create links with shortcodes like {{ link lesson-slug subheading }}
function indexLessonFiles() {
let allLessons = []
const sectionFolderNames = fs.readdirSync(CONTENT_DIR)
for (let sectionFolderName of sectionFolderNames) {
if (['images', '.DS_Store', '_index.md'].includes(sectionFolderName)) continue
const sectionFolderPath = `${CONTENT_DIR}/${sectionFolderName}`
const sectionIndex = readText(`${sectionFolderPath}/_index.md`)
const { data: sectionFrontmatter } = matter(sectionIndex)
const lessonFileNames = fs.readdirSync(sectionFolderPath)
for (let lessonFileName of lessonFileNames) {
const lessonFilePath = `${sectionFolderPath}/${lessonFileName}`
if (fs.lstatSync(lessonFilePath).isDirectory()) continue
if (['.DS_Store'].includes(lessonFileName)) continue
let lessonText = readText(lessonFilePath)
const { data: frontmatter, content } = matter(lessonText)
let lesson = {
slug: slugify(frontmatter.title), // frontmatter.slug,
sectionSlug: sectionFrontmatter.slug,
fileName: lessonFileName.replace('.md', ''),
}
allLessons.push(lesson)
}
}
console.log('Indexed lessons', allLessons);
return allLessons
}

function searchFiles(currentPath, callback) {
const files = fs.readdirSync(currentPath)
for (let fileName of files) {
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import fs from 'fs-extra'
import path from 'path'
import slugifyLib from 'slugify'

export function slugify(str) {
return slugifyLib(str, {
replacement: '-',
lower: true,
strict: true,
})
}

export async function loopOverFolders(parentFolderPath, callback) {
const folderNames = fs.readdirSync(parentFolderPath)
Expand Down

0 comments on commit 1a29bd2

Please sign in to comment.