This is a Livecoding Recap – an almost-weekly post about interesting things discovered while livecoding. Usually shorter than 500 words. Often with pictures. Livecoding happens almost every Sunday at 2pm PDT on multiple channels. You should follow My Youtube channel to catch me live.
I did it! The gatsby-transform-remark
bug I've been working on for a month yields to my assault o/
Not because it's that difficult of a bug. Oh no. Because I was doing dumb things and jumping into a large project like Gatsby is hard.
Previous attempts failed first because I couldn't get my dev environment to work and then because I made a newbie mistake with yarn.lock
and got frustrated. 😅
👉 https://swizec.com/blog/livecoding-recap-48-why-contributing-to-big-opensource-projects-is-still-hard/
👉 https://swizec.com/blog/livecoding-recap-50-how-newbie-mistakes-kill-the-flow/
The bug was more of a feature, really. When Remark generates a table of contents out of your markdown headers, it uses relative links. Like this 👇
# Heading
## Subheading 1
## Subheading 2
### Subsubheading
## Subheading 3
That turns into an HTML list with #heading
, #subheading-1
(etc) links. Nesting at all.
- Heading (#heading)
- Subheading 1 (#subheading-1)
- Subheading 2 (#subheading-2)
- Subsubheading (#subsubheading)
- Subheading 3 (#subheading-3)
Imagine all of that is HTML with <ul>
and <li>
and <a>
tags. Laying it out like this is easier to read.
This works great when you're only putting tables of contents on each individual page. Render a markdown document, put a TOC on top. Perfect.
But it stops working if you want to make a main table of contents for all your documents. All those relative links stop working. Subheading
is on an actual path now.
The fix was to prepend slugs to every URL that mdast-util-toc
generates. Best place to do that was deep inside gatsby-transform-remark
, in a file called extend-node-type.js
.
As far as I can tell, that file is the whole of the markdown transformer. It takes a markdown GraphQL node (flat text at this point) and extends it into something Gatsby can read to make a React-based HTML page.
Our fix went into the aptly named getTableOfContents
function.
const addSlugToUrl = function (node) {
if (node.url) {
node.url = withPrefix(`${markdownNode.fields.slug}${node.url}`)
}
if (node.children) {
node.children = node.children.map((node) => addSlugToUrl(node))
}
return node
}
tocAst.map = addSlugToUrl(tocAst.map)
This happens after all the processing that turns a markdown file into an abstract syntax tree (AST). Basic recursion 👇
- get node
- if node has
url
, change it - if node has children, call
addSlugToUrl
to each
Done 😁
Looks simple now that it works, but it sure took a while. Large codebases are fun like that.
Now just gotta figure out how to get absolute absolute paths working. You know, when you're not deploying your site to /
but to /something/
. Something to do with the --prefix-paths
flag when compiling.
Thanks to @lukeed05 for rubber ducking, and thanks to @kylemathews for answering questions when I got stuck. ❤️
Continue reading about Livecoding 51: I did it! My first PR to a big OSS project \o/
Semantically similar articles hand-picked by GPT-4
- Livecoding Recap 48- Why contributing to big opensource projects is still hard
- Livecoding Recap 50: How newbie mistakes kill the flow
- Using YouTube as a data source in Gatsbyjs
- Upgrading to Gatsby v2 with the help of the hivemind 👌
- Building a small CMS with NextJS, pt2 – CodeWithSwiz
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below 👇
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help 👉 swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers 👉 ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️