Skip to content

Commit

Permalink
Fix regex to capture images with alt text.
Browse files Browse the repository at this point in the history
  • Loading branch information
lumenwrites committed May 17, 2023
1 parent 1dbe0c0 commit 13179c7
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const OUTPUT_DIR = `${WORKING_DIR}/content-gdschool-processed`
const RELEASES_DIR = `${WORKING_DIR}/content-gdschool-releases`
let config


async function main() {
loadConfig()
let courseIndexText = readText(`${CONTENT_DIR}/_index.md`)
Expand Down Expand Up @@ -78,7 +77,7 @@ function parseConfig(config) {
function rewriteLinks(lessonText, courseUrl) {
// 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;
const linkRegex = /{{\s*link\s+([\w-]+)\s*([\w-]*)\s*}}/g

lessonText = lessonText.replace(linkRegex, (match, fileName, headingSlug) => {
const modifiedLink = `[${fileName}](${courseUrl}/${fileName}/${fileName})}`
Expand All @@ -89,10 +88,10 @@ function rewriteLinks(lessonText, courseUrl) {

// Replace image paths to absolute ones.
function rewriteImagePaths(lessonText, imagePathPrefix) {
const markdownImagePathRegex = /!\[\]\((.+?)\)/g
lessonText = lessonText.replace(markdownImagePathRegex, (match, imagePath) => {
const markdownImagePathRegex = /!\[(.*?)\]\((.+?)\)/g
lessonText = lessonText.replace(markdownImagePathRegex, (match, altText, imagePath) => {
const modifiedImagePath = `${imagePathPrefix}/${imagePath}`
return `![](${modifiedImagePath})`
return `![${altText}](${modifiedImagePath})`
})
const htmlImagePathRegex = /<img src="(.+?)"(.+?)\/>/g
lessonText = lessonText.replace(htmlImagePathRegex, (match, imagePath, attributes) => {
Expand Down

0 comments on commit 13179c7

Please sign in to comment.