Skip to content

Commit

Permalink
Process links.
Browse files Browse the repository at this point in the history
  • Loading branch information
lumenwrites committed May 16, 2023
1 parent c861044 commit 6845554
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# content-gdschool/
godot-node-essentials/
godot-node-essentials-bak/
godot-node-essentials-bak2/
content-gdschool-processed/
content-gdschool-releases/
process-course-macos
Expand Down
1 change: 1 addition & 0 deletions godot-node-essentials
33 changes: 22 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ async function main() {
console.log(`Processing lesson: ${lessonFileName}`)
let lessonText = readText(lessonFilePath)
const { data: lessonFrontmatter } = matter(lessonText)


// Process the content of the lesson - rewrite image paths, replace shortcodes, etc.
const imagePathPrefix = `/courses/${courseFrontmatter.slug}/${sectionFrontmatter.slug}`
lessonText = rewriteImagePaths(lessonText, imagePathPrefix)
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}`)

// Saving the processed lesson, in place.
saveText(lessonFilePath, lessonText)
Expand All @@ -65,22 +64,26 @@ async function main() {

function parseConfig(config) {
return config.split('\n').reduce((output, line) => {
const match = line.match(/(\w+)\s*=\s*"([^"]+)"/);
const match = line.match(/(\w+)\s*=\s*"([^"]+)"/)
if (match) {
const [_, key, values] = match;
output[key] = values.split(',').map(value => value.trim());
const [_, key, values] = match
output[key] = values.split(',').map((value) => value.trim())
}
return output;
}, {});
return output
}, {})
}

//
//
function rewriteLinks(lessonText, courseUrl) {
const linkRegex = /{{\s*link\s+([^\s{}]+)\s*}}/g;
lessonText = lessonText.replace(linkRegex, (match, fileName) => {
// 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;

lessonText = lessonText.replace(linkRegex, (match, fileName, headingSlug) => {
const modifiedLink = `[${fileName}](${courseUrl}/${fileName}/${fileName})}`
return modifiedLink
})
return lessonText
}

// Replace image paths to absolute ones.
Expand Down Expand Up @@ -138,7 +141,15 @@ function processCodeblocks(lessonText, lessonFileName, codeFiles) {
let codeText = readText(codeFilePath)
updatedContent = codeText
// If it has anchor tags, extract the text between them
if (anchor) updatedContent = extractTextBetweenAnchors(codeText, anchor)
try {
if (anchor) updatedContent = extractTextBetweenAnchors(codeText, anchor)
} catch (error) {
let errorMessage = `Error extracting text between anchors.\n`
errorMessage += `Lesson: ${lessonFileName}\n`
errorMessage += `Anchor: ${anchor}\n`
errorMessage += `Code file: ${codeFilePath}\n`
throw new Error(errorMessage)
}
updatedContent = removeAnchorTags(updatedContent)
// updatedContent = trimBlankLines(updatedContent)
return updatedContent
Expand Down

0 comments on commit 6845554

Please sign in to comment.