Skip to content

Commit

Permalink
fix section numbers, pull links out of lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
btholt committed Mar 22, 2020
1 parent e65c9dd commit fac26bd
Show file tree
Hide file tree
Showing 8 changed files with 7,754 additions and 2,911 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
26 changes: 13 additions & 13 deletions csv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require("fs").promises;
const path = require("path");
const fm = require("front-matter");
const isUrl = require("is-url-superb");
const parseLinks = require("parse-markdown-links");
const { sorter } = require("./helpers");
const mdDir = process.env.MARKDOWN_DIR || path.join(__dirname, "lessons/");
const outputPath =
process.env.OUTPUT_CSV_PATH || path.join(__dirname, "public/lessons.csv");
Expand Down Expand Up @@ -31,21 +34,18 @@ async function createCsv() {
const attributes = Array.from(seenAttributes.values());

if (attributes.includes("order")) {
frontmatters = frontmatters.sort(
// (a, b) => a.attributes.order - b.attributes.order
(a, b) => {
const [aSection, aLesson] = a.order.split(".");
const [bSection, bLesson] = b.order.split(".");

if (aSection !== bSection) {
return aSection - bSection;
}

return aLesson.charCodeAt() - bLesson.charCodeAt();
}
);
frontmatters = frontmatters.sort(sorter);
}

// get links
attributes.push("links");
seenAttributes.add("links");
frontmatters = frontmatters.map(row => {
const links = parseLinks(row.body).filter(isUrl);
row.attributes.links = JSON.stringify(links, 0, null);
return row;
});

// get all data into an array
let rows = frontmatters.map(item => {
const row = attributes.map(attr =>
Expand Down
34 changes: 34 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function splitSections(str) {
const validSectionTest = /^\d+[A-Z]+$/;
const numbersRegex = /^\d+/;
const lettersRegex = /[A-Z]+$/;
if (!validSectionTest.test(str)) {
throw new Error(
`${str} does not match the section format. It must be <numbers><capital letters>, like 16A or 5F (case sensitive)`
);
}

return [numbersRegex.exec(str)[0], lettersRegex.exec(str)[0]];
}

const getCharScore = str =>
str
.split("")
.map((char, index) => char.charCodeAt(0) * 10 ** index)
.reduce((acc, score) => acc + score);

function sorter(a, b) {
const [aSec, aSub] = splitSections(a.attributes.order);
const [bSec, bSub] = splitSections(b.attributes.order);

// sections first
if (aSec !== bSec) {
return aSec - bSec;
}

// subsections next
return getCharScore(aSub) - getCharScore(bSub);
}

module.exports.splitSections = splitSections;
module.exports.sorter = sorter;
9 changes: 8 additions & 1 deletion lessons/intro.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
path: "/intro"
title: "Introduction"
order: 1.A
order: "1A"
section: "The First Section"
description: "this is the description that will show up in social shares"
---
Expand All @@ -17,3 +17,10 @@ This is page one.
```js
const x = 2 + 2;
```

- This is a link to [Frontend Masters][fem].
- This another link to [Brian Holt's Twitter](https://twitter.com/holtbt).
- Here's another link to a [site built with this starter][containers].

[fem]: https://www.frontendmasters.com
[containers]: https://btholt.github.io/complete-intro-to-containers/
2 changes: 1 addition & 1 deletion lessons/page-2.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
path: "/page-2"
title: "Page 2"
order: 2.A
order: "2A"
description: "The second page"
section: "The Second Section"
---
Expand Down
9 changes: 9 additions & 0 deletions lessons/page-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
path: "/page-3"
title: "Page 3"
order: "2B"
description: "The third page"
section: "The Second Section"
---

This is page three.
Loading

0 comments on commit fac26bd

Please sign in to comment.