feat(frontmatter): separate frontmatter & content

Added function to read a file and separate frontmatter from content.
This commit is contained in:
Gosha Tcherednitchenko 2024-02-02 22:36:01 +00:00
parent 9be455b2b7
commit 6cf6bd2c0a
2 changed files with 21 additions and 1 deletions

View File

@ -1,3 +1,6 @@
---
title: This is a test document
---
# Test document
This is a test document that we should be able to convert to HTML.

View File

@ -2,4 +2,21 @@
(:use :cl))
(in-package :homestead)
;; blah blah blah.
(defun separate-content-and-frontmatter (file-path)
(with-open-file (stream file-path)
(let* ((line (read-line stream nil nil))
(in-frontmatter (string= line "---"))
frontmatter
content)
(setf frontmatter (with-output-to-string (s)
(when in-frontmatter
(loop while in-frontmatter do
(setf line (read-line stream nil nil))
(if (string= line "---")
(setf in-frontmatter nil)
(write-line line s))))))
(setf content (with-output-to-string (s)
(loop with end-of-file = nil
while (setf line (read-line stream nil end-of-file))
do (write-line line s))))
(list frontmatter content))))