MdTextRender
MdTextRender renders markdown into an existing jsPDF document.
Signature
ts
function MdTextRender(
doc: jsPDF,
text: string,
options: RenderOption
): Promise<void>Parameters
| Parameter | Type | Description |
|---|---|---|
doc | jsPDF | Existing jsPDF document instance |
text | string | Raw markdown input |
options | RenderOption | Render and security configuration |
Render Pipeline
mermaid
flowchart LR
A[Validate options] --> B[Security input limits]
B --> C[Parse markdown]
C --> D[Security tree limits]
D --> E[Apply link policy]
E --> F[Prefetch and validate images]
F --> G[Render tokens]
G --> H[Decorations and callback]Stage Notes
validateOptionsnormalizes defaults (includingsecurity).enforceMarkdownLimitschecks markdown length.MdTextParsertokenizes markdown.enforceNestedDepthAndImageCountsanitizes the parsed tree based on security limits.applyLinkPolicyvalidates links and applies placeholder/skip behavior.prefetchImagesvalidates and loads image data.- Renderer dispatches token-by-token to component renderers.
endCursorYHandlerreceives final cursor Y position.
Security Behavior
When security.enabled is true, MdTextRender can:
- block unsafe URLs and image sources
- enforce resource and structural limits
- switch behavior via
violationMode
Violation Modes
skip: continue rendering but omit blocked content.throw: abort withSecurityViolationError.placeholder: replace blocked content with placeholder text where supported.
Concurrency
MdTextRender is safe for concurrent usage across documents because render state is isolated per call.
Example
ts
import { jsPDF } from 'jspdf'
import { MdTextRender } from 'jspdf-md-renderer'
const doc = new jsPDF({ unit: 'mm', format: 'a4' })
await MdTextRender(doc, '# Hello\n\nWorld', {
cursor: { x: 10, y: 10 },
page: {
maxContentWidth: 190,
maxContentHeight: 277,
lineSpace: 1.5,
defaultLineHeightFactor: 1.2,
defaultFontSize: 12,
defaultTitleFontSize: 14,
topmargin: 10,
xpading: 10,
xmargin: 10,
indent: 10,
},
font: {
bold: { name: 'helvetica', style: 'bold' },
regular: { name: 'helvetica', style: 'normal' },
light: { name: 'helvetica', style: 'light' },
},
security: {
enabled: true,
violationMode: 'skip',
},
endCursorYHandler: (y) => console.log('Done at Y:', y),
})