Created
November 10, 2019 17:17
-
-
Save endiliey/a6c04442957faa4b2ec5903b250a6282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matter from 'gray-matter'; | |
import {createHash} from 'crypto'; | |
import _ from 'lodash'; | |
import escapeStringRegexp from 'escape-string-regexp'; | |
import fs from 'fs-extra'; | |
import readline from 'readline'; | |
import {once} from 'events'; | |
function isSeparator(line: string) { | |
// the '---' separator can have trailing whitespace but not leading whitespace | |
return line[0] === "-" && line.trim() === "---"; | |
} | |
export async function parseStream(filepath: string, highWaterMark: number = 1024) { | |
try { | |
const stream = fs.createReadStream(filepath, { | |
encoding: "utf8", | |
highWaterMark | |
}); | |
const rl = readline.createInterface({ | |
input: stream, | |
crlfDelay: Infinity | |
}); | |
// Note: we use the crlfDelay option to recognize all instances of CR LF | |
// ('\r\n') in input.txt as a single line break. | |
let isFirstLine = true; | |
let excerpt = ""; | |
let frontMatter: string[] = []; | |
let insideFrontMatter = true; | |
rl.on("line", line => { | |
// Process the line. | |
if (isFirstLine) { | |
isFirstLine = false; | |
if (isSeparator(line)) { | |
frontMatter.push(line); | |
} else { | |
excerpt = line; | |
rl.close(); | |
stream.destroy(); | |
} | |
} else { | |
if (insideFrontMatter) { | |
frontMatter.push(line); | |
// the next line is the first line of `body` | |
if (isSeparator(line)) { | |
insideFrontMatter = false; | |
} | |
} else if (line.length > 0 && excerpt === "") { | |
excerpt = line; | |
rl.close(); | |
stream.destroy(); | |
} | |
} | |
}); | |
await once(rl, "close"); | |
const { data } = matter(frontMatter.join("\n")); | |
return { frontMatter: data, excerpt }; | |
} catch (err) { | |
console.error(err); | |
return { frontMatter: {}, excerpt: ""} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment