dlog

package module
v0.0.0-...-504ff3d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2023 License: MIT Imports: 34 Imported by: 0

README

dlog

Go Report Card GoDoc Documentation

📼 dlog is a static site generator for digital gardening written in Go. It serves markdown files as HTML and allows editing files online. It focuses on enriching markdown files and surfacing implicit links between pages.

Documentation

License

dlog is released under MIT license

Cassette tape icons created by Good Ware - Flaticon

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SOURCE         string // path to markdown files directory
	BUILD          string // path to write built files
	READONLY       bool   // is dlog in readonly mode
	SITENAME       string // name of knowledgebase
	SIDEBAR        bool   // is sidebar displayed
	INDEX          string // name of the index page markdown file
	NOT_FOUND_PAGE string // name of the index page markdown file
)
View Source
var (

	// a function that renders CSRF hidden input field
	CSRF = csrf.TemplateField
)

The instance of markdown renderer. this is what takes the page content and converts it to HTML. it defines what features to use from goldmark and what options to turn on

Functions

func EachPage

func EachPage(ctx context.Context, f func(Page))

EachPage iterates on all available pages. many extensions uses it to get all pages and maybe parse them and extract needed information

func FindAllInAST

func FindAllInAST[t ast.Node](n ast.Node) (a []t)

Extract all nodes of a specific type from the AST

func FindInAST

func FindInAST[t ast.Node](n ast.Node) (found t, ok bool)

This is a function that takes an AST node and walks the tree depth first searching for a node of a specific type can be used to find first image, link, paragraph...etc

func FuncName

func FuncName(f any) string

func IgnoreDirectory

func IgnoreDirectory(r *regexp.Regexp)

IgnoreDirectory Register a pattern to be ignored when walking directories.

func Listen

func Listen(e PageEvent, h PageEventHandler)

Register an event handler to be executed when PageEvent is triggered. extensions can use this to register hooks under specific page events. extensions that keeps a cached version of the pages list for example needs to register handlers to update its cache

func Partial

func Partial(path string, data Locals) template.HTML

Partial executes a template by it's path name. it passes data to the template. returning the output of the template. in case of an error it will return the error string as the output

func RegisterAutocomplete

func RegisterAutocomplete(a Autocomplete)

RegisterAutocomplete registers an autocomplete function. it should be used by an extension to register a new autocomplete function. these functions are going to be executed when rendering the edit page.

func RegisterBuildPage

func RegisterBuildPage(p string, encloseInDir bool)

RegisterBuildPage registers a path of a page to export when building static version of the knowledgebase. encloseInDir will write the output to p/index.html instead instead of writing to p directly. that can help have paths with no .html extension to be served with the exact name.

func RegisterCommand

func RegisterCommand(c func(Page) []Command)

RegisterCommand registers a new command

func RegisterHelper

func RegisterHelper(name string, f interface{})

RegisterHelper registers a new helper function. all helpers are used when compiling templates. so registering helpers function must happen before the server starts as compiling templates happened right before starting the http server.

func RegisterLink(l func(Page) []Link)

Register a new links function, should return a list of Links

func RegisterPageSource

func RegisterPageSource(p PageSource)

func RegisterPreprocessor

func RegisterPreprocessor(f Preprocessor)

RegisterPreprocessor registers a Preprocessor function. extensions should use this function to register a preprocessor.

func RegisterProperty

func RegisterProperty(a func(Page) []Property)

RegisterProperty registers a function that returns a set of properties for the page

func RegisterQuickCommand

func RegisterQuickCommand(c func(Page) []Command)

func RegisterStaticDir

func RegisterStaticDir(f fs.FS)

RegisterStaticDir adds a filesystem to the filesystems list scanned for files when serving static files. can be used to add a directory of CSS or JS files by extensions

func RegisterTemplate

func RegisterTemplate(t fs.FS, subDir string)

RegisterTemplate registers a filesystem that contains templates, specifying subDir as the subdirectory name that contains the templates. templates are registered such that the latest registered directory override older ones. template file extensions are signified by '.html' extension and the file path can be used as template name without this extension

func RegisterWidget

func RegisterWidget(s WidgetSpace, priority float32, f WidgetFunc)

RegisterWidget Register a function to a widget space. functions registered will be executed in order of priority lower to higher when rendering view or edit page. the return values of these widgetfuncs will pass down to the template and injected in reserved places.

func RenderWidget

func RenderWidget(s WidgetSpace, p Page) (o template.HTML)

This is used by view and edit routes to render all widgetfuncs registered for specific widget space.

func Start

func Start()

Define the catch all HTTP routes, parse CLI flags and take actions like building the static pages and exit, or start the HTTP server

func Trigger

func Trigger(e PageEvent, p Page)

Trigger event handlers for a specific page event. page methods use this function to trigger all registered handlers when the page is edited or deleted for example.

func Vars

func Vars(r Request) map[string]string

Vars returns a map of key = dynamic segment in the path, value = the value of the segment registering a path like `"/edit/{page:.+}"` calling `Vars in the handler will return a map with one key `page` and the value is that part of the path in r

Types

type Autocomplete

type Autocomplete interface {
	StartChar() string
	Suggestions() []*Suggestion
}

Autocomplete defines what character triggeres the autocomplete feature and what is the list to display in this case.

type Command

type Command interface {
	// Icon returns the Fontawesome icon class name for the Command
	Icon() string
	// Name of the command. to be displayed in the list
	Name() string
	// Link returns the link/url/path of the command if any
	Link() string
	// OnClick action. a Javascript code to invoke when the command is executed
	OnClick() template.JS
	// Widget a HTML snippet to embed in the page that include any needed
	// assets, HTML, JS the command needs
	Widget() template.HTML
}

Command define a command that a user can invoke in view or edit page on a Page.

func Commands

func Commands(p Page) []Command

Commands return the list of commands for a page. it executes all functions registered with RegisterCommand and collect all results in one slice. result can be passed to the view to render the commands list

func QuickCommands

func QuickCommands(p Page) []Command

QuickCommands return the list of QuickCommands for a page. it executes all functions registered with RegisterQuickCommand and collect all results in one slice. result can be passed to the view to render the Quick commands list

type HandlerFunc

type HandlerFunc func(Response, Request) Output

HandlerFunc is the type of an HTTP handler function + returns output function. it makes it easier to return the output directly instead of writing the output to w then return.

type Link interface {
	// Icon returns the fontawesome icon class name or emoji
	Icon() string
	// Name returns the link text
	Name() string
	// Link returns the Href property for the link (URL, Path, ...etc)
	Link() string
}

Link represent link for the user interface, default theme renders it in the footer

func Links(p Page) []Link

Links returns a list of links for a Page. it executes all functions registered with RegisterLink and collect them in one slice. Can be passed to the view to render in the footer or sidebar for example.

type Locals

type Locals map[string]interface{} // passed to templates

map of string to any value used for template rendering

type Markdown

type Markdown string

Markdown is used instead of string to make sure it's clear the string is markdown string

func PreProcess

func PreProcess(content Markdown) Markdown

This function take the page content and pass it through all registered preprocessors and return the last preprocessor output to the caller

type Output

type Output = http.HandlerFunc

alias of http.HandlerFunc as output is expected from defined http handlers

func BadRequest

func BadRequest(msg string) Output

BadRequest returns an output function that writes BadRequest http response

func InternalServerError

func InternalServerError(err error) Output

InternalServerError returns an output function that writes InternalServerError http response

func JsonResponse

func JsonResponse(a any) Output

func NoContent

func NoContent() Output

NoContent returns an output function that writes NoContent http status

func NotFound

func NotFound(msg string) Output

NotFound returns an output function that writes 404 NotFound to http response

func PlainText

func PlainText(text string) Output

PlainText returns an output function that writes text to response writer

func Redirect

func Redirect(url string) Output

Redirect returns an output function that writes Found http response to provided URL

func Render

func Render(path string, data Locals) Output

Render returns an output function that renders partial with data and writes it as response

func Unauthorized

func Unauthorized(msg string) Output

Unauthorized returns an output function that writes Unauthorized http response

type Page

type Page interface {
	// Name returns page name without '.md' extension
	Name() string
	// returns the filename, makes sure it converts slashes to backslashes when
	// needed. this is safe to use when trying to access the file that represent the
	// page
	FileName() string
	// checks if the page underlying file exists on disk or not.
	Exists() bool
	// Renders the page content to HTML. it makes sure all preprocessors are called
	Render() template.HTML
	// Reads the underlying file and returns the content
	Content() Markdown
	// Deletes the file and makes sure it triggers the AfterDelete event
	Delete() bool
	// Overwrite page content with new content. making sure to trigger before and
	// after write events.
	Write(Markdown) bool
	// ModTime Return the last modification time of the underlying file
	ModTime() time.Time
	// Parses the page content and returns the Abstract Syntax Tree (AST).
	// extensions can use it to walk the tree and modify it or collect statistics or
	// parts of the page. for example the following "Emoji" function uses it to
	// extract the first emoji.
	AST() ast.Node
	// Returns the first emoji of the page.
	Emoji() string
}

a Type that represent a page.

func NewPage

func NewPage(name string) (p Page)

type PageEvent

type PageEvent int

a type used to define events to be used when the page is manipulated for example modified, renamed, deleted...etc.

const (
	BeforeWrite PageEvent = iota
	AfterWrite
	AfterDelete
)

List of page events. extensions can use these events to register a function to be executed when this event is triggered. extensions that require to be notified when the page is created or overwritten or deleted should register an event handler for the interesting events.

type PageEventHandler

type PageEventHandler func(Page) error

a function that handles a page event. this should be implemented by an extension and then registered. it will get executed when the event is triggered

type PageSource

type PageSource interface {
	// Page takes a page name and return a Page struct
	Page(string) Page
	// Each iterates over all pages in the source
	Each(context.Context, func(Page))
}

type Preprocessor

type Preprocessor func(Markdown) Markdown

A Preprocessor is a function that takes the whole page content and returns a modified version of the content. extensions should define this type and register is so that when page is rendered it will execute all of them in order like a pipeline each function output is passed as an input to the next. at the end the last preprocessor output is then rendered to HTML

type Property

type Property interface {
	// Icon returns the fontawesome icon class name or emoji
	Icon() string
	// Name returns the link text
	Name() string
}

Property represent a piece of information about the current page such as last update time, number of versions, number of words, reading time...etc

func Properties

func Properties(p Page) []Property

Properties return a list of properties for a page. It executes all functions registered with RegisterProperty and collect results in one slice. Can be passed to the view to render a page properties

type Request

type Request = *http.Request

alias *http.Request for shorter handler declaration

type Response

type Response = http.ResponseWriter

alias http.ResponseWriter for shorter handler declaration

type Route

type Route struct {
	// contains filtered or unexported fields
}

A route has handler function and set of RouteChecks if all checks are true, the last request will be passed to the handler function

func Delete

func Delete(path string, handler HandlerFunc, middlewares ...func(http.HandlerFunc) http.HandlerFunc) Route

Delete defines a new route that gets executed when the request matches path and method is http Delete. the list of middlewares are executed in order

func Get

func Get(path string, handler HandlerFunc, middlewares ...func(http.HandlerFunc) http.HandlerFunc) Route

Get defines a new route that gets executed when the request matches path and method is http Get. the list of middlewares are executed in order

func Match

func Match(route http.HandlerFunc, checks ...RouteCheck) Route

Match Adds a new HTTP handler function to the list of routes with a list of checks functions. the list of checks are executed when a request comes in if all of them returned true the handler function gets executed.

func Post

func Post(path string, handler HandlerFunc, middlewares ...func(http.HandlerFunc) http.HandlerFunc) Route

Post defines a new route that gets executed when the request matches path and method is http Post. the list of middlewares are executed in order

type RouteCheck

type RouteCheck func(Request) (Request, bool)

Checks a request for conditions, may modify request returning the new request and if conditions are met.

Can be used to check request method, path or other attributes against expected values.

type Suggestion

type Suggestion struct {
	Text        string // The text that gets injected in the editor if this option is chosen
	DisplayText string // The display text for this item in the menu. this can be more cosmetic. it has to start with the StartChar as the editor filter the list using this field
}

Suggestions represent an item in the list of autocomplete menu in the edit page

type WidgetFunc

type WidgetFunc func(Page) template.HTML

WidgetFunc a function that takes the current page and returns the widget. This can be used by extensions to define new widgets to be rendered in view or edit pages. the extension should define this func type and register it to be rendered in a specific widgetSpace such as before or after the page.

type WidgetSpace

type WidgetSpace string

WidgetSpace used to represent a widgets spaces. it's used to register widgets to be injected in the view or edit pages

var (
	AFTER_VIEW_WIDGET  WidgetSpace = "after_view"  // widgets rendered after the content of the view page
	BEFORE_VIEW_WIDGET WidgetSpace = "before_view" // widgets rendered before the content of the view page
	HEAD_WIDGET        WidgetSpace = "head"        // widgets rendered in page <head> tag
)

List of widgets spaces that extensions can use to register a WidgetFunc to inject content into.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL