All Projects → stephenafamo → goldmark-pdf

stephenafamo / goldmark-pdf

Licence: MIT License
A PDF renderer for the goldmark markdown parser.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to goldmark-pdf

markdown-toolbar
A clone of GitHub's markdown toolbar
Stars: ✭ 19 (-77.11%)
Mutual labels:  commonmark
CommonMarkEmoji
CommonMark extension adds UTF-8 emoji with Github tag for the `league/commonmark` PHP Markdown parsing engine, based on the CommonMark spec.
Stars: ✭ 17 (-79.52%)
Mutual labels:  commonmark
cmark-scala
Parse, manipulate and render CommonMark in Scala Native
Stars: ✭ 13 (-84.34%)
Mutual labels:  commonmark
paka.cmark
Lightweight CFFI-based Python bindings to cmark library (CommonMark implementation in C).
Stars: ✭ 23 (-72.29%)
Mutual labels:  commonmark
laravel-etched-blade
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
Stars: ✭ 19 (-77.11%)
Mutual labels:  commonmark
thegreatmarkdown
《了不起的 Markdown》
Stars: ✭ 44 (-46.99%)
Mutual labels:  commonmark
markdig.wpf
A WPF library for xoofx/markdig https://github.com/xoofx/markdig
Stars: ✭ 133 (+60.24%)
Mutual labels:  commonmark
NeoMarkdigXaml
Markdig Xaml/Wpf Renderer
Stars: ✭ 18 (-78.31%)
Mutual labels:  commonmark
commonmark-attributes-extension
The Attributes extension adds a syntax to define attributes on the various HTML elements in markdown’s output.
Stars: ✭ 32 (-61.45%)
Mutual labels:  commonmark
guile-commonmark
Implementation of CommonMark for Guile
Stars: ✭ 31 (-62.65%)
Mutual labels:  commonmark
swift-markdownkit
A framework for parsing and transforming text in Markdown format written in Swift 5 for macOS, iOS, and Linux. The supported syntax is based on the CommonMark specification. The framework defines an abstract syntax for Markdown, provides a parser for parsing strings into abstract syntax trees, and comes with generators for creating HTML and attr…
Stars: ✭ 64 (-22.89%)
Mutual labels:  commonmark
front-matter
The most featured front matter (yaml, json, neon, toml) parser and dumper for PHP.
Stars: ✭ 23 (-72.29%)
Mutual labels:  commonmark
Publish.jl
A universal document authoring package for Julia.
Stars: ✭ 86 (+3.61%)
Mutual labels:  commonmark
clj-pdf-markdown
Library for rendering markdown to clj-pdf data-structure syntax.
Stars: ✭ 20 (-75.9%)
Mutual labels:  commonmark
nwotable
One app for all your notes on all your devices.
Stars: ✭ 22 (-73.49%)
Mutual labels:  commonmark
markdown-it-github-headings
Add anchors and links to headings just like Github does
Stars: ✭ 22 (-73.49%)
Mutual labels:  commonmark
comark
Comark is CommonMark (Markdown) library for Haxe
Stars: ✭ 14 (-83.13%)
Mutual labels:  commonmark
NativeMarkKit
NativeMark is a flavor of Markdown designed to be rendered by native apps.
Stars: ✭ 36 (-56.63%)
Mutual labels:  commonmark
md-toc
Automatically generate a compliant table of contents for a markdown file to improve document readability
Stars: ✭ 18 (-78.31%)
Mutual labels:  commonmark
smu
Simple MarkUp - markdown/commonmark like syntax
Stars: ✭ 21 (-74.7%)
Mutual labels:  commonmark

goldmark-pdf

goldmark-pdf is a renderer for goldmark that allows rendering to PDF.

goldmark-pdf screenshot

Reference

See https://pkg.go.dev/github.com/stephenafamo/goldmark-pdf

Usage

Care has been taken to match the semantics of goldmark and its extensions.

The PDF renderer can be initiated with pdf.New() and the returned value satisfies goldmark's renderer.Renderer interface, so it can be passed to goldmark.New() using the goldmark.WithRenderer() option.

markdown := goldmark.New(
    goldmark.WithRenderer(pdf.New()),
)

Options can also be passed to pdf.New(), the options interface to be satisfied is:

// An Option interface is a functional option type for the Renderer.
type Option interface {
	SetConfig(*Config)
}

Here is the Config struct that is to be modified:

type Config struct {
	Context context.Context

	PDF PDF

	// A source for images
	ImageFS fs.FS

	// All other options have sensible defaults
	Styles Styles

	// A cache for the fonts
	FontsCache fonts.Cache

	// For debugging
	TraceWriter io.Writer

	NodeRenderers util.PrioritizedSlice
}

Some helper functions for adding options are already provided. See option.go

An example with some more options:

goldmark.New(
    goldmark.WithRenderer(
        pdf.New(
            pdf.WithTraceWriter(os.Stdout),
            pdf.WithContext(context.Background()),
            pdf.WithImageFS(os.DirFS(".")),
            pdf.WithLinkColor("cc4578"),
            pdf.WithHeadingFont(pdf.GetTextFont("IBM Plex Serif", pdf.FontLora)),
            pdf.WithBodyFont(pdf.GetTextFont("Open Sans", pdf.FontRoboto)),
            pdf.WithCodeFont(pdf.GetCodeFont("Inconsolata", pdf.FontRobotoMono)),
        ),
    ),
)

Fonts

The fonts that can be used in the PDF are based on the Font struct

// Represents a font.
type Font struct {
	CanUseForText bool
	CanUseForCode bool

	Category string
	Family   string

	FileRegular    string
	FileItalic     string
	FileBold       string
	FileBoldItalic string

	Type fontType
}

To be used for text, a font should have regular, italic, bold and bold-italic styles. Each of these has to be loaded separately.

To ease this process, variables have been generated for all the Google fonts that have these styles. For example:

var FontRoboto = Font{
	CanUseForCode:  false,
	CanUseForText:  true,
	Category:       "sans-serif",
	Family:         "Roboto",
	FileBold:       "700",
	FileBoldItalic: "700italic",
	FileItalic:     "italic",
	FileRegular:    "regular",
	Type:           fontTypeGoogle,
}

For codeblocks, if any other style is missing, the regular font is used in place.

var FontMajorMonoDisplay = Font{
	CanUseForCode:  true,
	CanUseForText:  false,
	Category:       "monospace",
	Family:         "Major Mono Display",
	FileBold:       "regular",
	FileBoldItalic: "regular",
	FileItalic:     "regular",
	FileRegular:    "regular",
	Type:           fontTypeGoogle,
}

When loading the fonts, they are downloaded on the fly using the fonts.

If you'd like to use a font outside of these, you should pass your own font struct which have been loaded into the PDF object you set in the Config. Be sure to set the FontType to FontTypeCustom so that we do not attempt to download it.

Contributing

Here's a list of things that I'd love help with:

  • More documentation
  • Testing
  • Finish the (currently buggy) implementation based on gopdf

License

MIT

Author

Stephen Afam-Osemene

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].