Newbie help required when trying to follow an outdated tutorial #8580
cuteiosdev
started this conversation in
General
Replies: 1 comment 5 replies
-
Hi Amy, turns out the issue was with the method importing and the property name of the result after parsing. // handle-markdown.js
import fs from 'fs';
// import files with pattern
import glob from "glob";
// parse front matter and body of markdown
import fm from "front-matter";
// parse body to html
- import unified from "unified";
+ import { unified } from "unified";
import remarkParse from 'remark-parse';
import remarkHtml from "remark-html";
import rehypePrism from "@mapbox/rehype-prism";
- import rehype from "rehype";
+ import { rehype } from "rehype";
/**
* import all markdown files in specified path, extract front matter and convert to html
* @param {string} markdownPath path to folder containing the markdown files (ends on /)
* @returns [{path, attributes, body}]
*/
export function importMarkdowns(markdownPath) {
let fileNames = glob.sync(`${markdownPath}*.md`);
return fileNames.map((path) => convertMarkdown(path));
}
/**
* convert markdown to object with attributes and html
* @param {string} path path to file
* @returns
*/
export function convertMarkdown(path) {
// read file
let file = fs.readFileSync(path, 'utf8');
console.log(path)
// extract frontmatter and body with the front-matter package
let { attributes, body } = fm(file);
console.log(attributes);
console.log(body);
// parse the body to html with the remark/rehype pipeline
let result = unified()
.use(remarkParse)
.use(remarkHtml)
.processSync(body)
- .contents;
+ .value;
console.log(result);
- result = rehype().use(rehypePrism).processSync(result).contents;
+ result = rehype().use(rehypePrism).processSync(result).value;
return { path, attributes, html: result};
}
export function convertToPostPreview(object) {
const url = object.path.replace(".md","").replace("src/", "");
return {...object.attributes, url};
} I've also created an updated and working example based on the linked tutorial. |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi lovely Svelte people, I'm trying to follow the tutorial at https://www.programonaut.com/how-to-create-a-blog-with-svelte-step-by-step/ though running into problems when trying to update it to make use of the 1.0 release and the migration guide at #5774.
Specifically the problem I'm having is with the markdown processing using unified / remark is giving the following error:
At the moment the
handle-markdown.js
file looks like the following for me as I'm trying to understand what's going on with the parsing:Would love some help identifying and tracking down what's happening so that I can continue working through the tutorial. Javascript / HTML is not my regular dev land (I'm an iOS developer) so likely not familiar with what terms to search for when looking for an answer.
Thanks,
Amy.
Beta Was this translation helpful? Give feedback.
All reactions