Skip to content

Commit 2a576d2

Browse files
RitaDiasrexxars
authored andcommitted
fix!: upgrade to refractor 5.0.0
BREAKING CHANGE: Module now uses refractor v5 under the hood
1 parent 7b30a40 commit 2a576d2

File tree

9 files changed

+66
-51
lines changed

9 files changed

+66
-51
lines changed

demo/demo.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom/client'
33

4-
import javascript from 'refractor/lang/javascript.js'
5-
import markup from 'refractor/lang/markup.js'
6-
import css from 'refractor/lang/css.js'
7-
import clike from 'refractor/lang/clike.js'
8-
import jsx from 'refractor/lang/jsx.js'
9-
104
import {Refractor, registerLanguage} from '../src/index.js'
5+
import javascript from 'refractor/javascript'
6+
import markup from 'refractor/markup'
7+
import css from 'refractor/css'
8+
import clike from 'refractor/clike'
9+
import jsx from 'refractor/jsx'
1110

1211
registerLanguage(javascript)
1312
registerLanguage(markup)

package-lock.json

Lines changed: 30 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"homepage": "https://github.com/rexxars/react-refractor#readme",
5353
"dependencies": {
54-
"refractor": "^4.8.1",
54+
"refractor": "^5.0.0",
5555
"unist-util-filter": "^5.0.1",
5656
"unist-util-visit-parents": "^6.0.1"
5757
},

src/Refractor.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {HTMLAttributes} from 'react'
22
import type {Syntax} from 'refractor'
3-
import {refractor as fract} from 'refractor/lib/core.js'
3+
import {refractor as fract} from 'refractor/core'
4+
import {Element as RefractorElement, Text} from 'hast'
45
import {addMarkers} from './addMarkers.js'
56
import {mapWithDepth} from './mapChildren.js'
67
import type {RefractorProps} from './types.js'
@@ -31,7 +32,10 @@ export function Refractor(props: RefractorProps) {
3132
ast = addMarkers(ast, {markers: props.markers})
3233
}
3334

34-
const value = ast.children.length === 0 ? props.value : ast.children.map(mapWithDepth(0))
35+
const value =
36+
ast.children.length === 0
37+
? props.value
38+
: (ast.children as (RefractorElement | Text)[]).map(mapWithDepth(0))
3539

3640
const code = <code {...codeProps}>{value}</code>
3741
return props.inline ? code : <pre className={preClass}>{code}</pre>

src/addMarkers.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {RefractorElement, RefractorRoot, Text} from 'refractor'
1+
import type {Element as RefractorElement, Root as RefractorRoot, Text} from 'hast'
22
import {filter} from 'unist-util-filter'
33
import {visitParents} from 'unist-util-visit-parents'
44

@@ -17,7 +17,7 @@ export function addMarkers(
1717
.map((marker) => (typeof marker === 'number' ? {line: marker} : marker))
1818
.sort((nodeA, nodeB) => nodeA.line - nodeB.line)
1919

20-
const numbered = lineNumberify(ast.children).nodes
20+
const numbered = lineNumberify(ast.children as (RefractorElement | Text)[]).nodes
2121
if (markers.length === 0 || numbered.length === 0) {
2222
return {...ast, children: numbered}
2323
}
@@ -58,7 +58,7 @@ function lineNumberify(ast: (RefractorElement | Text)[], context = {lineNumber:
5858
}
5959

6060
if (node.children) {
61-
const processed = lineNumberify(node.children, context)
61+
const processed = lineNumberify(node.children as (RefractorElement | Text)[], context)
6262
const firstChild = processed.nodes[0]
6363
const lastChild = processed.nodes[processed.nodes.length - 1]
6464
setLineInfo(
@@ -141,21 +141,24 @@ function unwrapLine(markerLine: number, nodes: (RefractorElement | Text)[]) {
141141
}
142142

143143
// These nodes are on previous lines, but nested within the same structure
144-
if (getLineStart(node) < markerLine) {
145-
addCopy(headMap, node, ancestors)
144+
if (getLineStart(node as RefractorElement | Text) < markerLine) {
145+
addCopy(headMap, node as Text, ancestors)
146146
return
147147
}
148148

149149
// These nodes are on the target line
150-
if (getLineStart(node) === markerLine) {
151-
addCopy(lineMap, node, ancestors)
150+
if (getLineStart(node as RefractorElement | Text) === markerLine) {
151+
addCopy(lineMap, node as Text, ancestors)
152152
return
153153
}
154154

155155
// If we have shared ancestors with some of the cloned elements,
156156
// create another tree of the remaining nodes
157-
if (getLineEnd(node) > markerLine && cloned.some((clone) => ancestors.includes(clone as any))) {
158-
addCopy(tailMap, node, ancestors)
157+
if (
158+
getLineEnd(node as RefractorElement | Text) > markerLine &&
159+
cloned.some((clone) => ancestors.includes(clone as any))
160+
) {
161+
addCopy(tailMap, node as Text, ancestors)
159162
}
160163
})
161164

@@ -222,13 +225,14 @@ function wrapLines(
222225
markers: Marker[],
223226
options: {markers: (Marker | number)[]},
224227
): RefractorRoot {
225-
const ast = markers.reduce((acc, marker) => unwrapLine(marker.line, acc), treeNodes)
226-
227-
// Container for the new AST
228+
const ast = markers.reduce<(RefractorElement | Text)[]>(
229+
(acc, marker) => unwrapLine(marker.line, acc) as (RefractorElement | Text)[],
230+
treeNodes.map((node) => node as RefractorElement | Text),
231+
)
228232
const wrapped: (RefractorElement | Text)[] = []
229233

230234
// Note: Markers are already sorted by line number (ascending)
231-
let astIndex = 0
235+
let astIndex: number = 0
232236
for (let m = 0; m < markers.length; m++) {
233237
const marker = markers[m]
234238

src/mapChildren.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {createElement, type JSXElementConstructor, type ReactElement} from 'react'
2-
import type {RefractorElement, Text} from 'refractor'
2+
import {Element as RefractorElement, Text} from 'hast'
33
import type {ReactRefractorMarkerDataWithComponent} from './types.js'
44

55
/**
@@ -29,7 +29,8 @@ function mapChild(child: RefractorElement | Text, i: number, depth: number): str
2929
}
3030

3131
const key = `fract-${depth}-${i}`
32-
const children = child.children && child.children.map(mapWithDepth(depth + 1))
32+
const children =
33+
child.children && (child.children as (RefractorElement | Text)[]).map(mapWithDepth(depth + 1))
3334

3435
if (!isReactRefractorMarkerDataWithComponent(child.data)) {
3536
return createElement(child.tagName, {key, className}, children)

test/Refractor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ReactDOM from 'react-dom/server'
2-
import haml from 'refractor/lang/haml'
3-
import js from 'refractor/lang/javascript'
2+
import haml from 'refractor/haml'
3+
import js from 'refractor/javascript'
44
import {beforeAll, expect, test} from 'vitest'
55

66
import {Refractor, RefractorProps, registerLanguage} from '../src'

test/addMarkers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {RefractorRoot} from 'refractor'
1+
import {Root as RefractorRoot} from 'hast'
22
import {expect, test} from 'vitest'
33
import {addMarkers} from '../src/addMarkers'
44
import {astFixtures} from './fixtures/ast'

test/fixtures/ast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {RefractorElement, Text} from 'refractor'
1+
import type {Element as RefractorElement, Text} from 'hast'
22

33
export const astFixtures: Array<RefractorElement | Text> = [
44
{

0 commit comments

Comments
 (0)