Skip to content

Instantly share code, notes, and snippets.

@endiliey
Created November 10, 2019 17:17
Show Gist options
  • Save endiliey/a6c04442957faa4b2ec5903b250a6282 to your computer and use it in GitHub Desktop.
Save endiliey/a6c04442957faa4b2ec5903b250a6282 to your computer and use it in GitHub Desktop.
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