@@ -9,38 +9,40 @@ Learn how to create custom transforms (plugins) for markdown-magic to extend its
99
1010## Transform Basics
1111
12- A transform is a function that takes content and options, then returns processed content:
12+ A transform is a function that takes a single API object and returns processed content:
1313
1414``` js
1515// Basic transform structure
16- function myTransform (content , options , config ) {
16+ function myTransform (api ) {
17+ const { content , options , settings } = api
1718 // Process the content
1819 return processedContent
1920}
2021```
2122
22- ### Parameters
23+ ### API Parameters
2324
2425- ` content ` : The content between the comment blocks
2526- ` options ` : Parsed options from the comment block
26- - ` config ` : Global markdown-magic configuration
27+ - ` srcPath ` : Current source markdown file path
28+ - ` settings ` : Global markdown-magic configuration
2729
2830## Creating Your First Transform
2931
3032### Simple String Transform
3133
3234``` js
3335// transforms/greeting.js
34- module .exports = function greeting (content , options ) {
36+ module .exports = function greeting ({ options } ) {
3537 const name = options .name || ' World'
3638 return ` Hello, ${ name} !`
3739}
3840```
3941
4042Usage:
4143``` md
42- <!-- doc-gen greeting name='Alice' -->
43- <!-- end-doc-gen -->
44+ <!-- docs greeting name='Alice' -->
45+ <!-- /docs -->
4446```
4547
4648Result: ` Hello, Alice! `
@@ -52,7 +54,7 @@ Result: `Hello, Alice!`
5254const fs = require (' fs' )
5355const path = require (' path' )
5456
55- module .exports = function include (content , options ) {
57+ module .exports = function include ({ content, options } ) {
5658 if (! options .src ) {
5759 throw new Error (' include transform requires "src" option' )
5860 }
@@ -70,8 +72,8 @@ module.exports = function include(content, options) {
7072
7173Usage:
7274``` md
73- <!-- doc-gen include src='./snippets/example.md' -->
74- <!-- end-doc-gen -->
75+ <!-- docs include src='./snippets/example.md' -->
76+ <!-- /docs -->
7577```
7678
7779## Async Transforms
@@ -82,7 +84,7 @@ For operations that require network requests or file system operations:
8284// transforms/fetchContent.js
8385const fetch = require (' node-fetch' )
8486
85- module .exports = async function fetchContent (content , options ) {
87+ module .exports = async function fetchContent ({ content, options } ) {
8688 if (! options .url ) {
8789 throw new Error (' fetchContent requires "url" option' )
8890 }
@@ -108,7 +110,7 @@ module.exports = async function fetchContent(content, options) {
108110// transforms/template.js
109111const mustache = require (' mustache' )
110112
111- module .exports = function template (content , options ) {
113+ module .exports = function template ({ content, options } ) {
112114 const template = options .template || content
113115 const data = {
114116 ... options .data ,
@@ -126,7 +128,7 @@ module.exports = function template(content, options) {
126128// transforms/codeStats.js
127129const fs = require (' fs' )
128130
129- module .exports = function codeStats (content , options ) {
131+ module .exports = function codeStats ({ options } ) {
130132 if (! options .src ) {
131133 throw new Error (' codeStats requires "src" option' )
132134 }
@@ -149,7 +151,7 @@ module.exports = function codeStats(content, options) {
149151
150152``` js
151153// transforms/tableOfContents.js
152- module .exports = function tableOfContents (content , options ) {
154+ module .exports = function tableOfContents ({ content, options } ) {
153155 const format = options .format || ' markdown'
154156 const maxDepth = parseInt (options .maxDepth ) || 3
155157 const minDepth = parseInt (options .minDepth ) || 1
@@ -194,7 +196,7 @@ function extractHeadings(content, minDepth, maxDepth) {
194196### In Configuration File
195197
196198``` js
197- // markdown .config.js
199+ // md .config.js
198200const greeting = require (' ./transforms/greeting' )
199201const include = require (' ./transforms/include' )
200202
@@ -215,7 +217,7 @@ import markdownMagic from 'markdown-magic'
215217
216218const config = {
217219 transforms: {
218- myTransform : (content , options ) => {
220+ myTransform : ({ content, options } ) => {
219221 return ` Processed: ${ content} `
220222 }
221223 }
@@ -231,7 +233,7 @@ markdownMagic('./docs/*.md', config)
231233Markdown-magic automatically parses options from the comment block:
232234
233235``` md
234- <!-- doc-gen myTransform
236+ <!-- docs myTransform
235237 stringOption='value'
236238 numberOption=42
237239 boolOption=true
@@ -242,7 +244,7 @@ Markdown-magic automatically parses options from the comment block:
242244
243245Access in transform:
244246``` js
245- module .exports = function myTransform (content , options ) {
247+ module .exports = function myTransform ({ options } ) {
246248 console .log (options .stringOption ) // 'value'
247249 console .log (options .numberOption ) // 42
248250 console .log (options .boolOption ) // true
@@ -254,7 +256,7 @@ module.exports = function myTransform(content, options) {
254256### Default Options
255257
256258``` js
257- module .exports = function myTransform (content , options ) {
259+ module .exports = function myTransform ({ options } ) {
258260 const defaults = {
259261 format: ' markdown' ,
260262 includeTitle: true ,
@@ -272,7 +274,7 @@ module.exports = function myTransform(content, options) {
272274### Validation
273275
274276``` js
275- module .exports = function strictTransform (content , options ) {
277+ module .exports = function strictTransform ({ options } ) {
276278 // Required options
277279 const required = [' url' , ' format' ]
278280 for (const opt of required) {
@@ -294,7 +296,7 @@ module.exports = function strictTransform(content, options) {
294296### Graceful Degradation
295297
296298``` js
297- module .exports = async function robustTransform (content , options ) {
299+ module .exports = async function robustTransform ({ content, options } ) {
298300 try {
299301 // Attempt primary operation
300302 return await primaryOperation (options)
@@ -353,8 +355,8 @@ describe('transform integration', () => {
353355 beforeEach (() => {
354356 // Reset test file
355357 fs .writeFileSync (testFile, `
356- <!-- doc-gen greeting name='Test' -->
357- <!-- end-doc-gen -->
358+ <!-- docs greeting name='Test' -->
359+ <!-- /docs -->
358360 ` .trim ())
359361 })
360362
0 commit comments