Skip to content
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

Rename type Range to MarkdownRange #464

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[];
}
Loading