greghendershott / markdown

Markdown parser written in Racket.
101 stars 28 forks source link

Table of contents rendering does not work #70

Closed ZelphirKaltstahl closed 6 years ago

ZelphirKaltstahl commented 6 years ago

I installed the markdown library from raco pkg install markdown (2017-11-11).

On the documentation page I see a procedure toc which shall give anxexpr with an added table of contents.

However, it does not seem to work for me. For the following two markdown examples …

Example 1:

# Test

This is a test post.

## Topic

sdada

Example 2:

# Test 2

| left | center | right |
|:-----|:------:|------:|
| a    | b      | c     |

… I only get back div (using (display ...) to print on terminal before further using the value).

Here is the code I use:

(define (read-post-from-file path)
  (display "path is:") (display path) (newline)
  (let ([result (car (parse-markdown path))])
    (display result) (newline)
    result))

Where path is for example:

#<path:data/posts/test.md>
greghendershott commented 6 years ago

Does the example in the documentation work for you? (I double-checked and it still works, for me.)

toc takes a list of x-expressions, for example what parse-markdown returns. It just looks for 'h1, 'h2 etc. elements and makes a 'div x-expression with a simple table of contents.

#lang racket

(require markdown
         markdown/toc)

(define src (string-join '("# 1" "## 1.1" "# 2" "## 2.1") "\n\n"))

(parse-markdown src)
;; =>
;; '((h1 ((id "1")) "1")
;;   (h2 ((id "11")) "1.1")
;;   (h1 ((id "2")) "2")
;;   (h2 ((id "21")) "2.1"))

(toc (parse-markdown src))
;; =>
;; '(div
;;   ((class "toc"))
;;   (ol
;;    (li (a ((href "#1")) "1") (ul (li (a ((href "#11")) "1.1"))))
;;    (li (a ((href "#2")) "2") (ul (li (a ((href "#21")) "2.1"))))))
ZelphirKaltstahl commented 6 years ago

I got it. The mistake was in how I handled the markdown input with(parse-markdown ...). I was using (car ...) on it, but that is only the first thing in the markdown file. I originally added that, because I thought I'd get a single xexpr from parse-markdown and saw in an output, that I got a list with one element (from an even simpler markdown file). To get the first xexpr I then added the car thingy.

Thanks for making things clear and helping!

The toc functionality is great. If it had not been there, I might have implemented it myself in probably much more laborious way.

greghendershott commented 6 years ago

Glad to hear!