Skip to content

Commit 36fffbb

Browse files
committed
chore(docs): add documentation for setLinkTransformer
1 parent 819902a commit 36fffbb

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,61 @@ const result = n2m.blockToMarkdown(block);
313313
// Result will now only use custom parser if the embed url matches a specific url
314314
```
315315

316+
## Link Transformers
317+
318+
The `linkTransformer` functionality allows you to customize how links are transformed when converting Notion blocks to Markdown. This is particularly useful if you want to modify the appearance or behavior of links in the generated Markdown.
319+
320+
### Usage
321+
322+
To use a link transformer, you can define a custom function that takes a link block and returns a string. This function can be set using the `setLinkTransformer` method.
323+
324+
### Example
325+
326+
```javascript
327+
const { Client } = require("@notionhq/client");
328+
const { NotionToMarkdown } = require("notion-to-md");
329+
330+
const notion = new Client({
331+
auth: "your integration token",
332+
});
333+
334+
const n2m = new NotionToMarkdown({ notionClient: notion });
335+
336+
// Set a custom link transformer
337+
n2m.setLinkTransformer((text, href) => {
338+
// Custom HTML link with additional attributes
339+
return `<a href="${href}" target="_blank" rel="noopener">${text}</a>`;
340+
});
341+
342+
// Or use it to modify the link behavior
343+
n2m.setLinkTransformer((text, href) => {
344+
// Add custom tracking or modify URLs
345+
const trackingUrl = `${href}?utm_source=notion&utm_medium=markdown`;
346+
return `[${text}](${trackingUrl})`;
347+
});
348+
349+
(async () => {
350+
const mdblocks = await n2m.pageToMarkdown("target_page_id");
351+
const mdString = n2m.toMarkdownString(mdblocks);
352+
console.log(mdString.parent);
353+
})();
354+
```
355+
356+
**Default behavior** (without custom transformer):
357+
```
358+
[Link text](https://example.com)
359+
```
360+
361+
**With custom HTML transformer**:
362+
```
363+
<a href="https://example.com" target="_blank" rel="noopener">Link text</a>
364+
```
365+
366+
**With custom tracking transformer**:
367+
```
368+
[Link text](https://example.com?utm_source=notion&utm_medium=markdown)
369+
```
370+
316371
## Contribution
317372

318373
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

0 commit comments

Comments
 (0)