htmy.md.core
MD
Bases: Snippet
Component for reading, customizing, and rendering markdown documents.
It supports all the processing utilities of Snippet
, including text_resolver
and
text_processor
for formatting, token replacement, and slot conversion to components.
One note regaring slot convesion (text_resolver
): it is executed before markdown parsing,
and all string segments of the resulting component sequence are parsed individually by the
markdown parser. As a consequence, you should only use slots in places where the preceding
and following texts individually result in valid markdown.
Source code in htmy/md/core.py
__init__(path_or_text, text_resolver=None, *, converter=None, renderer=None, text_processor=None)
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path_or_text
|
Text | str | Path
|
The path where the markdown file is located or a markdown |
required |
text_resolver
|
TextResolver | None
|
An optional |
None
|
converter
|
Callable[[str], Component] | None
|
Function that converts an HTML string (the parsed and processed markdown text) into a component. |
None
|
renderer
|
MarkdownRenderFunction | None
|
Function that gets the parsed and converted content and the metadata (if it exists) and turns them into a component. |
None
|
text_processor
|
TextProcessor | None
|
An optional text processors that can be used to process the text content before rendering. It can be used for example for token replacement or string formatting. |
None
|
Source code in htmy/md/core.py
MarkdownParser
Bases: ContextAware
Context-aware markdown parser.
By default, this class uses the markdown
library with a sensible set of
extensions including code highlighing.
Source code in htmy/md/core.py
_default = None
class-attribute
The default instance or None
if one hasn't been created already.
__init__(md=None)
Initialization.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
md
|
MarkdownParserFunction | None
|
The parser function to use. |
None
|
_default_md()
Function that creates the default markdown parser.
Returns:
Type | Description |
---|---|
MarkdownParserFunction
|
The default parser function. |
Source code in htmy/md/core.py
default()
classmethod
parse(text)
Returns the markdown data by parsing the given text.
htmy.md.typing
MarkdownParserFunction = Callable[[str], ParsedMarkdown]
module-attribute
Callable that converts a markdown string into a ParsedMarkdown
object.
MarkdownRenderFunction = Callable[[Component, MarkdownMetadataDict | None], Component]
module-attribute
Renderer function definition for markdown data.