-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
refactor(markdown): split markdown utils into separate files #8192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| export * from './Extension.js' | ||
| export * from './MarkdownManager.js' | ||
| export * from './utils.js' | ||
| export * from './utils/assumeContentType.js' | ||
| export * from './utils/closeMarksBeforeNode.js' | ||
| export * from './utils/extractAbsorbedBlankLines.js' | ||
| export * from './utils/findMarksToClose.js' | ||
| export * from './utils/findMarksToCloseAtEnd.js' | ||
| export * from './utils/findMarksToOpen.js' | ||
| export * from './utils/isTaskItem.js' | ||
| export * from './utils/reopenMarksAfterNode.js' | ||
| export * from './utils/wrapInMarkdownBlock.js' |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { Content } from '@tiptap/core' | ||
| import type { Fragment, Node } from '@tiptap/pm/model' | ||
|
|
||
| import type { ContentType } from '../types.js' | ||
|
|
||
| /** | ||
| * Assumes the content type based off the content. | ||
| * @param content The content to assume the type for. | ||
| * @param contentType The content type that should be prioritized. | ||
| */ | ||
| export function assumeContentType( | ||
| content: (Content | Fragment | Node) | string, | ||
| contentType: ContentType, | ||
| ): ContentType { | ||
| // if not a string, we assume it will be a json content object | ||
| if (typeof content !== 'string') { | ||
| return 'json' | ||
| } | ||
|
|
||
| // otherwise we let the content type be what it is | ||
| return contentType | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Closes active marks before rendering a non-text node. | ||
| * Returns the closing markdown syntax and clears the active marks. | ||
| */ | ||
| export function closeMarksBeforeNode( | ||
| activeMarks: Map<string, any>, | ||
| getMarkClosing: (markType: string, mark: any) => string, | ||
| ): string { | ||
| let beforeMarkdown = '' | ||
| Array.from(activeMarks.keys()) | ||
| .reverse() | ||
| .forEach(markType => { | ||
| const mark = activeMarks.get(markType) | ||
| const closeMarkdown = getMarkClosing(markType, mark) | ||
| if (closeMarkdown) { | ||
| beforeMarkdown = closeMarkdown + beforeMarkdown | ||
| } | ||
| }) | ||
| activeMarks.clear() | ||
| return beforeMarkdown | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { MarkdownToken } from '@tiptap/core' | ||
|
|
||
| /** | ||
| * Matches a run of two or more consecutive line breaks (a blank line) at the | ||
| * end of a string, allowing horizontal whitespace between the breaks. | ||
| * | ||
| * @example | ||
| * TRAILING_BLANK_LINES.test('paragraph\n \n') | ||
| * // => true | ||
| */ | ||
| const TRAILING_BLANK_LINES = /\n[^\S\n]*(?:\n[^\S\n]*)+$/ | ||
|
|
||
| /** | ||
| * Extracts blank lines absorbed into marked token `raw` back into explicit | ||
| * `space` tokens so they're handled uniformly by the reconstruction path. | ||
| * @param tokens The marked token stream to normalize. | ||
| * @returns A new token array with absorbed blank lines as `space` tokens. | ||
| */ | ||
| export function extractAbsorbedBlankLines(tokens: MarkdownToken[]): MarkdownToken[] { | ||
| return tokens.flatMap((token, index) => | ||
| extractAbsorbedBlankLinesFromToken(token, tokens[index + 1]), | ||
| ) | ||
| } | ||
|
|
||
| function extractAbsorbedBlankLinesFromToken( | ||
| token: MarkdownToken, | ||
| nextToken: MarkdownToken | undefined, | ||
| ): MarkdownToken[] { | ||
| // A following `space` token already carries the blank lines for this gap. | ||
| if (token.type === 'space' || nextToken?.type === 'space') { | ||
| return [token] | ||
| } | ||
|
|
||
| const trailingBlankLines = (token.raw || '').match(TRAILING_BLANK_LINES) | ||
|
|
||
| if (!trailingBlankLines) { | ||
| return [token] | ||
| } | ||
|
|
||
| return [ | ||
| { ...token, raw: (token.raw || '').slice(0, -trailingBlankLines[0].length) }, | ||
| { type: 'space', raw: trailingBlankLines[0] } as MarkdownToken, | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.