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 2 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
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1053,13 +1053,13 @@ PODS:
- React-jsi (= 0.73.4)
- React-logger (= 0.73.4)
- React-perflogger (= 0.73.4)
- RNLiveMarkdown (0.1.96):
- RNLiveMarkdown (0.1.121):
- glog
- hermes-engine
- RCT-Folly (= 2022.05.16.00)
- React-Core
- RNLiveMarkdown/common (= 0.1.96)
- RNLiveMarkdown/common (0.1.96):
- RNLiveMarkdown/common (= 0.1.121)
- RNLiveMarkdown/common (0.1.121):
- glog
- hermes-engine
- RCT-Folly (= 2022.05.16.00)
Expand Down Expand Up @@ -1278,7 +1278,7 @@ SPEC CHECKSUMS:
React-runtimescheduler: ed48e5faac6751e66ee1261c4bd01643b436f112
React-utils: 6e5ad394416482ae21831050928ae27348f83487
ReactCommon: 840a955d37b7f3358554d819446bffcf624b2522
RNLiveMarkdown: ffa97e63f100bd32fad2f6f3b689031188761578
RNLiveMarkdown: 522e631ecd54c207f3bdbd5e87edf8a549eefdb7
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 64cd2a583ead952b0315d5135bf39e053ae9be70

Expand Down
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