-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix custom parsing on the web (#585)
- Loading branch information
Showing
3 changed files
with
61 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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,56 @@ | ||
import type {MarkdownRange, MarkdownType} from './commonTypes'; | ||
|
||
// getTagPriority returns a priority for a tag, higher priority means the tag should be processed first | ||
function getTagPriority(tag: string) { | ||
switch (tag) { | ||
case 'blockquote': | ||
return 2; | ||
case 'h1': | ||
return 1; | ||
default: | ||
return 0; | ||
} | ||
} | ||
|
||
function sortRanges(ranges: MarkdownRange[]) { | ||
// sort ranges by start position, then by length, then by tag hierarchy | ||
return ranges.sort((a, b) => a.start - b.start || b.length - a.length || getTagPriority(b.type) - getTagPriority(a.type) || 0); | ||
} | ||
|
||
function groupRanges(ranges: MarkdownRange[]) { | ||
const lastVisibleRangeIndex: {[key in MarkdownType]?: number} = {}; | ||
|
||
return ranges.reduce((acc, range) => { | ||
const start = range.start; | ||
const end = range.start + range.length; | ||
|
||
const rangeWithSameStyleIndex = lastVisibleRangeIndex[range.type]; | ||
const sameStyleRange = rangeWithSameStyleIndex !== undefined ? acc[rangeWithSameStyleIndex] : undefined; | ||
|
||
if (sameStyleRange && sameStyleRange.start <= start && sameStyleRange.start + sameStyleRange.length >= end && range.length > 1) { | ||
// increment depth of overlapping range | ||
sameStyleRange.depth = (sameStyleRange.depth || 1) + 1; | ||
} else { | ||
lastVisibleRangeIndex[range.type] = acc.length; | ||
acc.push(range); | ||
} | ||
|
||
return acc; | ||
}, [] as MarkdownRange[]); | ||
} | ||
|
||
function ungroupRanges(ranges: MarkdownRange[]): MarkdownRange[] { | ||
const ungroupedRanges: MarkdownRange[] = []; | ||
ranges.forEach((range) => { | ||
if (!range.depth) { | ||
ungroupedRanges.push(range); | ||
} | ||
const {depth, ...rangeWithoutDepth} = range; | ||
Array.from({length: depth!}).forEach(() => { | ||
ungroupedRanges.push(rangeWithoutDepth); | ||
}); | ||
}); | ||
return ungroupedRanges; | ||
} | ||
|
||
export {sortRanges, groupRanges, ungroupRanges}; |
This file contains 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