Skip to content

Commit

Permalink
Rename type Range to MarkdownRange (#464)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekzaw authored Aug 31, 2024
1 parent c6f0408 commit f3033b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions parser/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {expect} from '@jest/globals';
import type {Range} from '../index';
import type {MarkdownRange} from '../index';

require('../react-native-live-markdown-parser.js');

declare module 'expect' {
interface Matchers<R> {
toBeParsedAs(expectedRanges: Range[]): R;
toBeParsedAs(expectedRanges: MarkdownRange[]): R;
}
}

const toBeParsedAs = function (actual: string, expectedRanges: Range[]) {
const toBeParsedAs = function (actual: string, expectedRanges: MarkdownRange[]) {
const actualRanges = global.parseExpensiMarkToRanges(actual);
if (JSON.stringify(actualRanges) !== JSON.stringify(expectedRanges)) {
return {
Expand Down
16 changes: 8 additions & 8 deletions parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ExpensiMark from 'expensify-common/dist/ExpensiMark';
import {unescapeText} from './utils';

type MarkdownType = 'bold' | 'italic' | 'strikethrough' | 'emoji' | 'mention-here' | 'mention-user' | 'mention-report' | 'link' | 'code' | 'pre' | 'blockquote' | 'h1' | 'syntax';
type Range = {
type MarkdownRange = {
type: MarkdownType;
start: number;
length: number;
Expand Down Expand Up @@ -87,7 +87,7 @@ function parseTokensToTree(tokens: Token[]): StackItem {
return stack[0]!;
}

function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
function parseTreeToTextAndRanges(tree: StackItem): [string, MarkdownRange[]] {
let text = '';

function processChildren(node: StackItem | string) {
Expand All @@ -109,7 +109,7 @@ function parseTreeToTextAndRanges(tree: StackItem): [string, Range[]] {
ranges.push({type, start, length: end - start});
}

const ranges: Range[] = [];
const ranges: MarkdownRange[] = [];
function dfs(node: StackItem | string) {
if (typeof node === 'string') {
text += node;
Expand Down Expand Up @@ -226,12 +226,12 @@ function getTagPriority(tag: string) {
}
}

function sortRanges(ranges: Range[]) {
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: Range[]) {
function groupRanges(ranges: MarkdownRange[]) {
const lastVisibleRangeIndex: {[key in MarkdownType]?: number} = {};

return ranges.reduce((acc, range) => {
Expand All @@ -250,10 +250,10 @@ function groupRanges(ranges: Range[]) {
}

return acc;
}, [] as Range[]);
}, [] as MarkdownRange[]);
}

function parseExpensiMarkToRanges(markdown: string): Range[] {
function parseExpensiMarkToRanges(markdown: string): MarkdownRange[] {
try {
const html = parseMarkdownToHTML(markdown);
const tokens = parseHTMLToTokens(html);
Expand All @@ -276,4 +276,4 @@ function parseExpensiMarkToRanges(markdown: string): Range[] {
}

globalThis.parseExpensiMarkToRanges = parseExpensiMarkToRanges;
export type {MarkdownType, Range};
export type {MarkdownType, MarkdownRange};
4 changes: 2 additions & 2 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {};

type MarkdownType = 'bold' | 'italic' | 'strikethrough' | 'emoji' | 'mention-here' | 'mention-user' | 'mention-report' | 'link' | 'code' | 'pre' | 'blockquote' | 'h1' | 'syntax';

type Range = {
type MarkdownRange = {
type: MarkdownType;
start: number;
length: number;
Expand All @@ -11,5 +11,5 @@ type Range = {

declare global {
// eslint-disable-next-line no-var
var parseExpensiMarkToRanges: (markdown: string) => Range[];
var parseExpensiMarkToRanges: (markdown: string) => MarkdownMarkdownRange[];
}

0 comments on commit f3033b9

Please sign in to comment.