markdown

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAsideJSX

func BuildAsideJSX(ad *AsideSegment) string

BuildAsideJSX renders an admonition as an <Aside> component JSX string. The rendered markdown body is passed as a dangerouslySetInnerHTML div so it is emitted as raw HTML (unescaped) inside the Aside.

func BuildCodeJSX

func BuildCodeJSX(lang, code string) string

BuildCodeJSX renders a fenced code block as a <Code> component JSX string, passing the raw code as template-literal children so build-time chroma highlighting and the client-side copy/header UI both apply.

func ExtractImports

func ExtractImports(src string) []string

ExtractImports pulls import statements from MDX/TSX source lines. Returns only lines that start with "import " (after trimming whitespace).

func HTMLToJSX

func HTMLToJSX(html string) string

HTMLToJSX converts rendered HTML to JSX-compatible syntax. Specifically converts void elements to self-closing tags.

func HasAsideSegments

func HasAsideSegments(segments []MDXSegment) bool

HasAsideSegments reports whether any segment is an <Aside> block (used to decide whether to auto-import the Aside component).

func HasCodeSegments

func HasCodeSegments(segments []MDXSegment) bool

HasCodeSegments reports whether any segment is a <Code> block (used to decide whether to auto-import the Code component).

func RenderSegmentsToHTML

func RenderSegmentsToHTML(segments []MDXSegment) string

RenderSegmentsToHTML renders parsed segments back into a single HTML string, suitable for a search index or prerendered fallback content. Code blocks render as plain <pre>, and admonitions as <aside>-structure markup matching the <Aside> component (without client-side icons).

func RenderToHTML

func RenderToHTML(src string, cfg Config) string

RenderToHTML converts Markdown source to HTML using the given config.

Types

type AsideSegment

type AsideSegment struct {
	Type      string // admonition type: note | tip | warning | danger | caution
	Title     string // resolved title (Note, Tip, ...) passed explicitly
	InnerHTML string // rendered markdown for the admonition body
}

AsideSegment holds an admonition (:::type ... :::) captured from markdown so it can be rendered as the <Aside> component instead of Go-generated markup.

type CodeSegment

type CodeSegment struct {
	Lang string // fenced code block language (may be empty)
	Code string // raw code content
}

CodeSegment holds a fenced code block captured from markdown so it can be rendered as the <Code> component (with copy button + header) instead of a plain static <pre>.

type Config

type Config struct {
	Root           string `json:"root"`           // root directory of the project, used for resolving relative paths
	GFM            bool   `json:"gfm"`            // GitHub Flavored Markdown (tables, task lists, strikethrough)
	HeadingAnchors bool   `json:"headingAnchors"` // auto-generate id attributes on headings
	Admonitions    bool   `json:"admonitions"`    // :::note, :::tip, :::warning, :::danger
	CodeHighlight  bool   `json:"codeHighlight"`  // syntax highlighting for code blocks
	CodeTheme      string `json:"codeTheme"`      // chroma theme name for syntax highlighting (default: github-dark)
	Math           bool   `json:"math"`           // $...$ and $$...$$ math support
}

func DefaultConfig

func DefaultConfig() Config

type JSXBlock

type JSXBlock struct {
	Index    int    // position in the output order
	Tag      string // component name
	Attrs    string // raw attributes string
	Children string // content between opening and closing tags
}

JSXBlock represents a JSX component embedded in MDX content.

type MDXResult

type MDXResult struct {
	Frontmatter map[string]string
	HTML        string
	JSXBlocks   []JSXBlock
}

MDXResult holds the parsed result of an MDX file.

func ParseMDX

func ParseMDX(src string, cfg Config) *MDXResult

ParseMDX splits an MDX source into frontmatter, Markdown content, and JSX blocks. Returns the frontmatter map and the rendered HTML with JSX placeholders.

func (*MDXResult) ReinsertJSXBlocks

func (r *MDXResult) ReinsertJSXBlocks() string

ReinsertJSXBlocks replaces placeholders in HTML with the actual JSX block content.

type MDXSegment

type MDXSegment struct {
	HTML  string        // rendered HTML for markdown parts
	JSX   string        // JSX source for JSX blocks (e.g., "<CustomComponent>test</CustomComponent>")
	Code  *CodeSegment  // fenced code block, rendered as <Code> component
	Aside *AsideSegment // admonition (:::note), rendered as <Aside> component
}

MDXSegment represents either HTML text, a JSX block, or a code block for TSX generation.

func ParseMDXSegments

func ParseMDXSegments(src string, cfg Config) (frontmatter map[string]string, segments []MDXSegment)

ParseMDXSegments parses MDX (or plain Markdown) and returns ordered segments (HTML + JSX blocks + code blocks) suitable for generating TSX source code. JSX blocks are returned as raw JSX strings, and fenced code blocks are returned as Code segments that render the <Code> component — both are embedded directly in TSX output.

Jump to

Keyboard shortcuts

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