Skip to content

Commit cbee331

Browse files
committed
Add resolve helper to state
1 parent 8d29193 commit cbee331

File tree

7 files changed

+39
-38
lines changed

7 files changed

+39
-38
lines changed

lib/handlers/a.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../state.js').State} State
55
*/
66

7-
import {resolve} from '../util/resolve.js'
8-
97
/**
108
* @param {State} state
119
* State.
@@ -20,7 +18,7 @@ export function a(state, node) {
2018
/** @type {Link} */
2119
const result = {
2220
type: 'link',
23-
url: resolve(state, String(properties.href || '') || null),
21+
url: state.resolve(String(properties.href || '') || null),
2422
title: properties.title ? String(properties.title) : null,
2523
// @ts-expect-error: assume valid children.
2624
children: state.all(node)

lib/handlers/iframe.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../state.js').State} State
55
*/
66

7-
import {resolve} from '../util/resolve.js'
8-
97
/**
108
* @param {State} state
119
* State.
@@ -28,7 +26,7 @@ export function iframe(state, node) {
2826
const result = {
2927
type: 'link',
3028
title: null,
31-
url: resolve(state, src),
29+
url: state.resolve(src),
3230
children: [{type: 'text', value: title}]
3331
}
3432
state.patch(node, result)

lib/handlers/img.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @typedef {import('../state.js').State} State
55
*/
66

7-
import {resolve} from '../util/resolve.js'
8-
97
/**
108
* @param {State} state
119
* State.
@@ -20,7 +18,7 @@ export function img(state, node) {
2018
/** @type {Image} */
2119
const result = {
2220
type: 'image',
23-
url: resolve(state, String(properties.src || '') || null),
21+
url: state.resolve(String(properties.src || '') || null),
2422
title: properties.title ? String(properties.title) : null,
2523
alt: properties.alt ? String(properties.alt) : ''
2624
}

lib/handlers/input.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import {findSelectedOptions} from '../util/find-selected-options.js'
11-
import {resolve} from '../util/resolve.js'
1211

1312
const defaultChecked = '[x]'
1413
const defaultUnchecked = '[ ]'
@@ -53,7 +52,7 @@ export function input(state, node) {
5352
/** @type {Image} */
5453
const result = {
5554
type: 'image',
56-
url: resolve(state, String(properties.src || '') || null),
55+
url: state.resolve(String(properties.src || '') || null),
5756
title: String(properties.title || '') || null,
5857
alt: String(alt)
5958
}
@@ -102,7 +101,7 @@ export function input(state, node) {
102101
let index = -1
103102

104103
while (++index < values.length) {
105-
const value = resolve(state, values[index][0])
104+
const value = state.resolve(values[index][0])
106105
/** @type {Link} */
107106
const result = {
108107
type: 'link',

lib/handlers/media.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import {toString} from 'mdast-util-to-string'
1111
import {visit, EXIT} from 'unist-util-visit'
12-
import {resolve} from '../util/resolve.js'
1312
import {wrapNeeded} from '../util/wrap.js'
1413

1514
/**
@@ -62,7 +61,7 @@ export function media(state, node) {
6261
const image = {
6362
type: 'image',
6463
title: null,
65-
url: resolve(state, poster),
64+
url: state.resolve(poster),
6665
alt: toString(nodes)
6766
}
6867
state.patch(node, image)
@@ -74,7 +73,7 @@ export function media(state, node) {
7473
const result = {
7574
type: 'link',
7675
title: properties.title ? String(properties.title) : null,
77-
url: resolve(state, src),
76+
url: state.resolve(src),
7877
// @ts-expect-error Assume phrasing content.
7978
children: nodes
8079
}

lib/state.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Transform the children of a hast parent to mdast.
2222
* @property {One} one
2323
* Transform a hast node to mdast.
24+
* @property {Resolve} resolve
25+
* Resolve a URL relative to a base.
2426
* @property {Options} options
2527
* User configuration.
2628
* @property {Map<string, Element>} elementById
@@ -60,6 +62,13 @@
6062
* Parent of `node`.
6163
* @returns {MdastNode | Array<MdastNode> | void}
6264
* mdast result.
65+
*
66+
* @callback Resolve
67+
* Resolve a URL relative to a base.
68+
* @param {string | null | undefined} url
69+
* Possible URL value.
70+
* @returns {string}
71+
* URL, resolved to a `base` element, if any.
6372
*/
6473

6574
import {position} from 'unist-util-position'
@@ -79,6 +88,7 @@ export function createState(options) {
7988
/** @type {State} */
8089
const state = {
8190
patch,
91+
resolve,
8292
all,
8393
one,
8494
options,
@@ -197,3 +207,25 @@ function all(parent) {
197207
? results
198208
: results.slice(start, end)
199209
}
210+
211+
/**
212+
* @this {State}
213+
* Info passed around about the current state.
214+
* @param {string | null | undefined} url
215+
* Possible URL value.
216+
* @returns {string}
217+
* URL, resolved to a `base` element, if any.
218+
*/
219+
export function resolve(url) {
220+
const base = this.frozenBaseUrl
221+
222+
if (url === null || url === undefined) {
223+
return ''
224+
}
225+
226+
if (base) {
227+
return String(new URL(url, base))
228+
}
229+
230+
return url
231+
}

lib/util/resolve.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)