Generate raw content for markdown files.

This commit is contained in:
Dillon Kearns 2019-08-02 06:29:34 -07:00
parent 34b29436fd
commit cbb918bc33
5 changed files with 113 additions and 18 deletions

View File

@ -10,12 +10,12 @@
"elm/core": "1.0.2",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm-community/list-extra": "8.2.1",
"lukewestby/elm-string-interpolate": "1.0.3"
},
"indirect": {
"elm/regex": "1.0.0",
"elm/virtual-dom": "1.0.2",
"elm-community/list-extra": "8.2.0"
"elm/virtual-dom": "1.0.2"
}
},
"test-dependencies": {

View File

@ -3,6 +3,7 @@ port module Main exposing (main)
import Cli.Option
import Cli.OptionsParser as OptionsParser exposing (with)
import Cli.Program as Program
import List.Extra
import String.Interpolate exposing (interpolate)
@ -29,7 +30,7 @@ generatePage pageOrPost =
prerenderRcFormattedPath : PageOrPost -> String
prerenderRcFormattedPath pageOrPost =
pageOrPost.path
|> String.dropRight 4
|> dropExtension
|> String.split "/"
|> dropIndexFromLast
|> List.drop 1
@ -76,7 +77,7 @@ prerenderPaths content =
pathFor : PageOrPost -> String
pathFor pageOrPost =
pageOrPost.path
|> String.dropRight 4
|> dropExtension
|> String.split "/"
|> List.drop 1
|> dropIndexFromLast
@ -85,8 +86,20 @@ pathFor pageOrPost =
|> (\list -> String.concat [ "[", list, "]" ])
generate : List PageOrPost -> String
generate content =
dropExtension : String -> String
dropExtension path =
if path |> String.endsWith ".emu" then
path |> String.dropRight 4
else if path |> String.endsWith ".md" then
path |> String.dropRight 3
else
path
generate : List PageOrPost -> List PageOrPost -> String
generate content markdownContent =
interpolate """module RawContent exposing (content)
import Pages.Content as Content exposing (Content)
@ -94,13 +107,71 @@ import Dict exposing (Dict)
import Element exposing (Element)
content : List ( List String, String )
content : { markdown : List ( List String, { frontMatter : String, body : String } ), markup : List ( List String, String ) }
content =
{ markdown = markdown, markup = markup }
markdown : List ( List String, { frontMatter : String, body : String } )
markdown =
[ {1}
]
markup : List ( List String, String )
markup =
[
{0}
]
"""
[ List.map generatePage content |> String.join "\n ,"
, List.map generateMarkdownPage markdownContent |> String.join "\n ,"
]
isFrontmatterDelimeter : Maybe String -> Bool
isFrontmatterDelimeter line =
line == Just "---"
splitMarkdown : String -> ( String, String )
splitMarkdown contents =
let
lines =
contents
|> String.lines
in
if lines |> List.head |> isFrontmatterDelimeter then
splitAtClosingDelimeter (lines |> List.drop 1)
else
( "", lines |> String.join "\n" )
splitAtClosingDelimeter : List String -> ( String, String )
splitAtClosingDelimeter lines =
List.Extra.splitWhen (\line -> line == "---") lines
|> Maybe.map (Tuple.mapSecond (List.drop 1))
|> Maybe.withDefault ( [], [] )
|> Tuple.mapBoth (String.join "\n") (String.join "\n")
generateMarkdownPage markdown =
let
( frontmatter, body ) =
splitMarkdown markdown.contents
in
-- interpolate """ ( [ "markdown" ], { frontMatter = "title: This is a markdown article\\n", body = "# Hey there 👋\\nWelcome to this markdown document!" } ) """
-- [ markdown.path ]
interpolate """( {0}
, { frontMatter = \"\"\"{1}
\"\"\"
, body = \"\"\"{2}\"\"\" }
)
"""
[ pathFor markdown
, frontmatter
, body
]
@ -122,7 +193,7 @@ type alias Flags =
type alias Extras =
{ content : List PageOrPost, images : List String }
{ content : List PageOrPost, markdownContent : List PageOrPost, images : List String }
type alias PageOrPost =
@ -132,7 +203,7 @@ type alias PageOrPost =
init : Flags -> CliOptions -> Cmd Never
init flags (Default watch) =
{ rawContent =
generate flags.content
generate flags.content flags.markdownContent
, prerenderrc = preRenderRc flags.content
, imageAssets = imageAssetsFile flags.images
, watch = watch

View File

@ -16,12 +16,19 @@ function unpackFile(path: string) {
function run() {
console.log("Running elm-pages...");
const content = glob.sync(contentGlobPath, {}).map(unpackFile);
const markdownContent = glob.sync("content/**/*.md", {}).map(unpackFile);
const images = glob
.sync("images/**/*", {})
.filter(imagePath => !fs.lstatSync(imagePath).isDirectory());
let app = Elm.Main.init({
flags: { argv: process.argv, versionMessage: version, content, images }
flags: {
argv: process.argv,
versionMessage: version,
content,
markdownContent,
images
}
});
app.ports.printAndExitSuccess.subscribe((message: string) => {

7
site/content/markdown.md Normal file
View File

@ -0,0 +1,7 @@
---
title: This is a markdown article
---
# Hey there 👋
Welcome to this markdown document!

View File

@ -1,8 +1,8 @@
module RawContent exposing (content)
import Pages.Content as Content exposing (Content)
import Dict exposing (Dict)
import Element exposing (Element)
import Pages.Content as Content exposing (Content)
content : { markdown : List ( List String, { frontMatter : String, body : String } ), markup : List ( List String, String ) }
@ -12,16 +12,23 @@ content =
markdown : List ( List String, { frontMatter : String, body : String } )
markdown =
[ ( [ "markdown" ], { frontMatter = """
title: This is a markdown article
""", body = """# Hey there 👋
Welcome to this markdown document!""" } )
[ ( ["markdown"]
, { frontMatter = """title: This is a markdown article
"""
, body = """
# Hey there 👋
Welcome to this markdown document!
""" }
)
]
markup : List ( List String, String )
markup =
[ ( [ "about" ]
[
( ["about"]
, """|> Article
title = How I Learned /elm-markup/
description = How I learned to use elm-markup.
@ -63,7 +70,8 @@ What does a *list* look like?
With some content
"""
)
, ( [ "articles" ]
,( ["articles"]
, """|> Article
title = How I Learned /elm-markup/
description = How I learned to use elm-markup.
@ -74,7 +82,8 @@ Here are some articles. You can learn more at.....
posts = articles
"""
)
, ( [ "articles", "moving-faster-with-tiny-steps" ]
,( ["articles", "moving-faster-with-tiny-steps"]
, """|> Article
title = Moving Faster with Tiny Steps in Elm
description = How I learned to use elm-markup.
@ -232,4 +241,5 @@ You can sign up here for more tips on writing Elm code incrementally. When you s
Let me know how this technique goes! Ive gotten a lot of great feedback from my clients about this approach, and I love hearing success stories. Hit reply and let me know how it goes! Id love to hear how youre able to apply this in your day-to-day work!
"""
)
]