Skip to content

Commit 3d639ba

Browse files
committed
TOC:
- Indent item based on header level - Documentation
1 parent ad243e8 commit 3d639ba

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This package depends on two other packages:
1616
- [Syntax highlighting (for code blocks)](#syntax-highlighting)
1717
- [Dark and light themes](#custom-themes)
1818
- [Customised themes (by passing a JSON file to `md2pdf`)](#custom-themes)
19+
- [Auto Generation of Table of Contents]()
1920
- [Support of non-Latin charsets and multiple fonts](#using-non-ascii-glyphsfonts)
2021
- [Pagination control (using horizontal lines - especially useful for presentations)](#additional-options)
2122
- [Page Footer (consisting of author, title and page number)](#additional-options)
@@ -61,6 +62,13 @@ For examples, see [testdata/syntax_highlighting.md](./testdata/syntax_highlighti
6162
However, if you wish to customise the font faces, sizes and colours, you can use the JSONs in
6263
[custom_themes](./custom_themes) as a starting point. Edit to your liking and pass `--theme /path/to/json` to `md2pdf`
6364

65+
## Auto Generation of Table of Contents
66+
67+
`md2pdf` can automatically generate a TOC (where each item corresponds to a header in the doc) and include it in the first page.
68+
These can then be clicked to navigate to the relevant section.
69+
70+
To make use of this feature, simple pass `--generate-toc` as argument.
71+
6472
## Quick start
6573

6674
```
@@ -104,6 +112,8 @@ annotation name must match the syntax base filename.*
104112
path to font file to use
105113
-font-name string
106114
Font name ID; e.g 'Helvetica-1251'
115+
-generate-toc
116+
Auto Generate Table of Contents (TOC)
107117
-help
108118
Show usage message
109119
-i string

cmd/md2pdf/md2pdf.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var fontName = flag.String("font-name", "", "Font name ID; e.g 'Helvetica-1251'"
2929
var themeArg = flag.String("theme", "light", "[light | dark | /path/to/custom/theme.json]")
3030
var hrAsNewPage = flag.Bool("new-page-on-hr", false, "Interpret HR as a new page; useful for presentations")
3131
var printFooter = flag.Bool("with-footer", false, "Print doc footer (<author> <title> <page number>)")
32-
var generateTOC = flag.Bool("generate-toc", false, "Auto Generate TOC")
32+
var generateTOC = flag.Bool("generate-toc", false, "Auto Generate Table of Contents (TOC)")
3333
var pageSize = flag.String("page-size", "A4", "[A3 | A4 | A5]")
3434
var orientation = flag.String("orientation", "portrait", "[portrait | landscape]")
3535
var logFile = flag.String("log-file", "", "Path to log file")
@@ -203,8 +203,11 @@ func main() {
203203
if linkPtr, exists := headerLinks[header.Title]; exists {
204204
link := *linkPtr
205205
pf.Pdf.SetFont("Arial", "", 12)
206+
pf.Pdf.SetTextColor(100, 149, 237)
206207
tr := pf.Pdf.UnicodeTranslatorFromDescriptor("")
207-
pf.Pdf.WriteLinkID(8, fmt.Sprintf("%s %s", tr("•"), header.Title), link)
208+
bulletChar := tr("•")
209+
indent := strings.Repeat(" ", header.Level-1)
210+
pf.Pdf.WriteLinkID(8, fmt.Sprintf("%s %s %s", indent, bulletChar, header.Title), link)
208211
pf.Pdf.Ln(15)
209212
}
210213
}

md2pdf.1.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ For examples, see `testdata/Markdown Documentation - Syntax.text` and `testdata/
3838
However, if you wish to customise the font faces, sizes and colours, you can use the JSONs in
3939
[custom_themes](./custom_themes) as a starting point. Edit to your liking and pass `--theme /path/to/json` to `md2pdf`
4040

41+
## Auto Generation of Table of Contents
42+
43+
`md2pdf` can automatically generate a TOC (where each item corresponds to a header in the doc) and include it in the first page.
44+
These can then be clicked to navigate to the relevant section.
45+
46+
To make use of this feature, simple pass `--generate-toc` as argument.
47+
4148
## Synopsis
4249

4350
```sh
@@ -47,6 +54,8 @@ However, if you wish to customise the font faces, sizes and colours, you can use
4754
path to font file to use
4855
-font-name string
4956
Font name ID; e.g 'Helvetica-1251'
57+
-generate-toc
58+
Auto Generate Table of Contents (TOC)
5059
-help
5160
Show usage message
5261
-i string

0 commit comments

Comments
 (0)