Skip to content

Commit f738812

Browse files
committed
docs(markdown): document markdown utility functions
Add @PARAM, @returns and runnable examples to the public markdown utility JSDoc.
1 parent 3fb114f commit f738812

8 files changed

Lines changed: 56 additions & 0 deletions

packages/markdown/src/utils/assumeContentType.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import type { ContentType } from '../types.js'
77
* Assume the content type based on the content value.
88
* @param content The content to assume the type for.
99
* @param contentType The content type that should be prioritized.
10+
* @returns 'json' when the content is not a string, otherwise the given contentType.
11+
* @example
12+
* assumeContentType({ type: 'paragraph' }, 'html') // => 'json'
13+
* assumeContentType('**bold**', 'markdown') // => 'markdown'
1014
*/
1115
export function assumeContentType(
1216
content: (Content | Fragment | Node) | string,

packages/markdown/src/utils/closeMarksBeforeNode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ import type { JSONMark } from '../types.js'
33
/**
44
* Closes active marks before rendering a non-text node.
55
* Returns the closing markdown syntax and clears the active marks.
6+
* @param activeMarks The marks to close.
7+
* @param getMarkClosing Returns the closing syntax for a mark.
8+
* @returns The concatenated closing syntax, innermost mark last.
9+
* @example
10+
* const active = new Map([['bold', { type: 'bold' }], ['italic', { type: 'italic' }]])
11+
* closeMarksBeforeNode(active, markType => (markType === 'bold' ? '**' : '*'))
12+
* // => '***' (italic closes before bold, LIFO)
613
*/
714
export function closeMarksBeforeNode(
815
activeMarks: Map<string, JSONMark>,

packages/markdown/src/utils/extractAbsorbedBlankLines.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const TRAILING_BLANK_LINES = /\n[^\S\n]*(?:\n[^\S\n]*)+$/
1212
* Recover blank lines absorbed into token `raw` as explicit `space` tokens.
1313
* @param tokens The marked token stream to normalize.
1414
* @returns A new token array with absorbed blank lines as `space` tokens.
15+
* @example
16+
* extractAbsorbedBlankLines([{ type: 'paragraph', raw: 'hello\n\n' }])
17+
* // => [{ type: 'paragraph', raw: 'hello' }, { type: 'space', raw: '\n\n' }]
1518
*/
1619
export function extractAbsorbedBlankLines(tokens: MarkdownToken[]): MarkdownToken[] {
1720
return tokens.flatMap((token, index) =>

packages/markdown/src/utils/findMarksToClose.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import type { JSONMark } from '../types.js'
66
/**
77
* Determines which marks to close based on the next node's marks,
88
* treating same-type marks with different attributes as distinct.
9+
* @param currentMarks The marks on the current text node.
10+
* @param nextNode The following node, or null when this is the last node.
11+
* @returns The mark types that do not continue on the next node.
12+
* @example
13+
* findMarksToClose(new Map([['bold', { type: 'bold' }]]), null)
14+
* // => ['bold']
15+
* findMarksToClose(new Map([['bold', { type: 'bold' }]]), {
16+
* type: 'text',
17+
* text: 'x',
18+
* marks: [{ type: 'bold' }],
19+
* })
20+
* // => [] (bold continues on the next node)
921
*/
1022
export function findMarksToClose(
1123
currentMarks: Map<string, JSONMark>,

packages/markdown/src/utils/findMarksToCloseAtEnd.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import type { JSONMark } from '../types.js'
66
/**
77
* Determines which marks to close at the node end, treating same-type marks
88
* with different attributes as distinct (close + reopen).
9+
* @param activeMarks The marks currently open before this node.
10+
* @param currentMarks The marks on the current text node.
11+
* @param nextNode The following node, or null when this is the last node.
12+
* @param markSetsEqual Callback comparing two mark sets for equality.
13+
* @returns The mark types to close at the end of the current node.
14+
* @example
15+
* const active = new Map([['bold', { type: 'bold' }], ['italic', { type: 'italic' }]])
16+
* findMarksToCloseAtEnd(active, active, null, () => false)
17+
* // => ['italic', 'bold'] (all active marks close, LIFO)
918
*/
1019
export function findMarksToCloseAtEnd(
1120
activeMarks: Map<string, JSONMark>,

packages/markdown/src/utils/findMarksToOpen.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ import type { JSONMark } from '../types.js'
55
/**
66
* Determines which marks need to open, treating same-type marks with
77
* different attributes as distinct (close + reopen).
8+
* @param activeMarks The marks already open.
9+
* @param currentMarks The marks on the current text node.
10+
* @returns The marks that are not yet active, or whose attributes differ.
11+
* @example
12+
* const active = new Map([['bold', { type: 'bold' }]])
13+
* findMarksToOpen(active, new Map([['bold', { type: 'bold' }], ['italic', { type: 'italic' }]]))
14+
* // => [{ type: 'italic', mark: { type: 'italic' } }]
815
*/
916
export function findMarksToOpen(
1017
activeMarks: Map<string, JSONMark>,

packages/markdown/src/utils/reopenMarksAfterNode.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import type { JSONMark } from '../types.js'
33
/**
44
* Reopens marks after rendering a non-text node.
55
* Returns the opening markdown syntax and updates the active marks.
6+
* @param marksToReopen The marks to reopen on the following text.
7+
* @param activeMarks The marks to restore.
8+
* @param getMarkOpening Returns the opening syntax for a mark.
9+
* @returns The concatenated opening syntax.
10+
* @example
11+
* const active = new Map()
12+
* reopenMarksAfterNode(
13+
* new Map([['bold', { type: 'bold' }], ['italic', { type: 'italic' }]]),
14+
* active,
15+
* () => '*',
16+
* )
17+
* // => '**'
618
*/
719
export function reopenMarksAfterNode(
820
marksToReopen: Map<string, JSONMark>,

packages/markdown/src/utils/wrapInMarkdownBlock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* @param prefix The prefix to wrap each line with.
44
* @param content The content to wrap.
55
* @returns The content with each line wrapped with the prefix.
6+
* @example
7+
* wrapInMarkdownBlock('> ', 'first\nsecond') // => '> first\n> \n> second'
68
*/
79
export function wrapInMarkdownBlock(prefix: string, content: string) {
810
// split content lines

0 commit comments

Comments
 (0)