Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Livecoding: I did it! My First PR to a Big OSS Project [Video]

DZone's Guide to

Livecoding: I did it! My First PR to a Big OSS Project [Video]

Working with large open source codebases can be tough; just ask this developer. Read the article and watch the livecoding to see the weird bits of code he ran into.

· Web Dev Zone
Free Resource

Get deep insight into Node.js applications with real-time metrics, CPU profiling, and heap snapshots with N|Solid from NodeSource. Learn more.


I did it! The gatsby-transform-remark bug I've been working on for a month yields to my assault!

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.

See my posts here and here

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. The 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.

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. 

Node.js application metrics sent directly to any statsd-compliant system. Get N|Solid

Topics:
web dev ,open source ,web application development ,pull requests

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}