Skip to content

Load markdown files through markdown-with-front-matter-loader #1304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

pugnascotia
Copy link
Contributor

This PR adds support for loading *.md files through markdown-with-front-matter-loader. This makes it simple to load and render Markdown through CRA.

Testing

  1. Create a demo app with ./tasks/cra.sh my-app-test
  2. cd my-app-test
  3. Create src/_posts/hello-world.md
---
title: 'Hello world'
---
# Hello world

This is a test

```javascript
function() { }
```
  1. Modify src/App.js to load the file
import post from './_pages/hello-world.md';
  1. Include the post in the page:
<p className="App-intro">
  To get started, edit <code>src/App.js</code> and save to reload.
</p>
<div dangerouslySetInnerHTML={{__html: post.__content}}></div>
  1. Start dev mode and check the page renders as expected.
yarn start
  1. Build a production bundle:
yarn run build
  1. Check that the production build works as expected by serving it and browsing http://localhost/
docker run -it --rm -v $PWD/build:/usr/share/nginx/html -p 80:80 nginx:alpine

@gaearon
Copy link
Contributor

gaearon commented Feb 12, 2017

Thanks for the PR! What does that JSON look like?

@gaearon
Copy link
Contributor

gaearon commented Feb 12, 2017

Also, can you provide node_modules size before and after this change?

@pugnascotia
Copy link
Contributor Author

Regarding node_modules size - test script:

#!/bin/bash

set -eo pipefail

test_branch() {
  branch="$1"
  git checkout "$branch" >/dev/null 2>&1
  rm -rf build
  npm run create-react-app build >/dev/null 2>&1
  ls build/node_modules > "$HOME/tmp/cra-$branch-modules.txt"
  du -s build/node_modules/
}

MASTER_SIZE=$(test_branch master)
MARKDOWN_SIZE=$(test_branch load-markdown)

echo "node_modules size on master: $MASTER_SIZE"
echo "node_modules size on load-markdown: $MARKDOWN_SIZE"
echo
echo "Module diff:"
diff -u "$HOME/tmp/cra-master-modules.txt" "$HOME/tmp/cra-load-markdown-modules.txt"

Results:

node_modules size on master: 292608	build/node_modules/
node_modules size on load-markdown: 293840	build/node_modules/

Module diff:
--- /Users/rah/tmp/cra-master-modules.txt	2017-02-13 14:49:27.000000000 +0000
+++ /Users/rah/tmp/cra-load-markdown-modules.txt	2017-02-13 14:50:02.000000000 +0000
@@ -487,6 +487,7 @@
 lru-cache
 macaddress
 makeerror
+markdown-with-front-matter-loader
 marked
 marked-terminal
 math-expression-evaluator
@@ -788,5 +789,6 @@
 xtend
 y18n
 yallist
+yaml-front-matter
 yargs
 yargs-parser

Size of new modules:

$ cd build/node_modules/
$ du -csh markdown-with-front-matter-loader yaml-front-matter
 24K	markdown-with-front-matter-loader
592K	yaml-front-matter
616K	total

@pugnascotia
Copy link
Contributor Author

Regarding JSON - given some Markdown:

---
title: Test markdown post
date: 2015-06-03
tags: Badger, Squirrel, Nihilist
---

# Content

Some content

You get the following, except the date below is actually a Date object. This is just a stringified version.

{
  "title": "Test markdown post",
  "date": "2015-06-03T00:00:00.000Z",
  "tags": "Badger, Squirrel, Nihilist",
  "__content": "<h1 id=\"content\">Content</h1>\n<p>Some content</p>\n"
}

@gaearon
Copy link
Contributor

gaearon commented Feb 13, 2017

What is the benefit of this structure, compared to regular Markdown -> HTML conversion?

@pugnascotia
Copy link
Contributor Author

The first part can contain any fields you want, so you can use whatever metainfo conventions make sense for your Markdown documents. For example, a tags field (string or YAML array) for categorising a blog post, a field for a blog post's background image, author name, references, anything you like really. You could even pre-process the files at build time to extract the metadata and build some kind of index.

That's all separate from the actual markdown content.

@gaearon
Copy link
Contributor

gaearon commented Feb 24, 2017

So, I appreciate the PR but I think we'll hold off for now. If we add an exception for .md now, people will ask the same for HTML and a bunch of other formats. At the same time I'm trying not to add more size for non-essential features.

If there are more requests for this in the future, we can reconsider this. Sorry!

@gaearon gaearon closed this Feb 24, 2017
@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants