-
Notifications
You must be signed in to change notification settings - Fork 10
/
core.cljc
38 lines (33 loc) · 1.12 KB
/
core.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns cybermonday.core
(:require
[cybermonday.utils :as utils]
[cybermonday.lowering :as lowering]
[cybermonday.ir :as ir]
#?(:org.babashka/nbb [clojure.core :refer [js->clj] :rename {js->clj ->clj}]
:cljs [cljs-bean.core :refer [->clj]])
#?(:clj [yaml.core :as yaml]
:cljs ["yaml" :as yaml])))
(def frontmatter-re #"(?ms)(?:^---$(.*)^---$)?(.*)")
(def parse-yaml #?(:clj yaml/parse-string
:cljs (comp ->clj yaml/parse)))
(defn parse-front
"Parse only the frontmatter of a markdown file."
[md]
(let [[_ fm _] (re-matches frontmatter-re md)]
(when fm (parse-yaml fm))))
(defn parse-body
"Parse only the body of a markdown file."
([md opts]
(let [[_ _ body] (re-matches frontmatter-re md)]
(-> body
ir/md-to-ir
(lowering/to-html-hiccup opts)
utils/cleanup)))
([md] (parse-body md nil)))
(defn parse-md
"Generates HTML hiccup from markdown and associated frontmatter
See `cybermonday.lowering/to-html-hiccup` for opts map values. "
([md opts]
{:frontmatter (parse-front md)
:body (parse-body md opts)})
([md] (parse-md md nil)))