Skip to content

Commit

Permalink
Add resolve helper to state
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 11, 2023
1 parent 8d29193 commit cbee331
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 38 deletions.
4 changes: 1 addition & 3 deletions lib/handlers/a.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../state.js').State} State
*/

import {resolve} from '../util/resolve.js'

/**
* @param {State} state
* State.
Expand All @@ -20,7 +18,7 @@ export function a(state, node) {
/** @type {Link} */
const result = {
type: 'link',
url: resolve(state, String(properties.href || '') || null),
url: state.resolve(String(properties.href || '') || null),
title: properties.title ? String(properties.title) : null,
// @ts-expect-error: assume valid children.
children: state.all(node)
Expand Down
4 changes: 1 addition & 3 deletions lib/handlers/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../state.js').State} State
*/

import {resolve} from '../util/resolve.js'

/**
* @param {State} state
* State.
Expand All @@ -28,7 +26,7 @@ export function iframe(state, node) {
const result = {
type: 'link',
title: null,
url: resolve(state, src),
url: state.resolve(src),
children: [{type: 'text', value: title}]
}
state.patch(node, result)
Expand Down
4 changes: 1 addition & 3 deletions lib/handlers/img.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* @typedef {import('../state.js').State} State
*/

import {resolve} from '../util/resolve.js'

/**
* @param {State} state
* State.
Expand All @@ -20,7 +18,7 @@ export function img(state, node) {
/** @type {Image} */
const result = {
type: 'image',
url: resolve(state, String(properties.src || '') || null),
url: state.resolve(String(properties.src || '') || null),
title: properties.title ? String(properties.title) : null,
alt: properties.alt ? String(properties.alt) : ''
}
Expand Down
5 changes: 2 additions & 3 deletions lib/handlers/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

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

const defaultChecked = '[x]'
const defaultUnchecked = '[ ]'
Expand Down Expand Up @@ -53,7 +52,7 @@ export function input(state, node) {
/** @type {Image} */
const result = {
type: 'image',
url: resolve(state, String(properties.src || '') || null),
url: state.resolve(String(properties.src || '') || null),
title: String(properties.title || '') || null,
alt: String(alt)
}
Expand Down Expand Up @@ -102,7 +101,7 @@ export function input(state, node) {
let index = -1

while (++index < values.length) {
const value = resolve(state, values[index][0])
const value = state.resolve(values[index][0])
/** @type {Link} */
const result = {
type: 'link',
Expand Down
5 changes: 2 additions & 3 deletions lib/handlers/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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

/**
Expand Down Expand Up @@ -62,7 +61,7 @@ export function media(state, node) {
const image = {
type: 'image',
title: null,
url: resolve(state, poster),
url: state.resolve(poster),
alt: toString(nodes)
}
state.patch(node, image)
Expand All @@ -74,7 +73,7 @@ export function media(state, node) {
const result = {
type: 'link',
title: properties.title ? String(properties.title) : null,
url: resolve(state, src),
url: state.resolve(src),
// @ts-expect-error Assume phrasing content.
children: nodes
}
Expand Down
32 changes: 32 additions & 0 deletions lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* Transform the children of a hast parent to mdast.
* @property {One} one
* Transform a hast node to mdast.
* @property {Resolve} resolve
* Resolve a URL relative to a base.
* @property {Options} options
* User configuration.
* @property {Map<string, Element>} elementById
Expand Down Expand Up @@ -60,6 +62,13 @@
* Parent of `node`.
* @returns {MdastNode | Array<MdastNode> | void}
* mdast result.
*
* @callback Resolve
* Resolve a URL relative to a base.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*/

import {position} from 'unist-util-position'
Expand All @@ -79,6 +88,7 @@ export function createState(options) {
/** @type {State} */
const state = {
patch,
resolve,
all,
one,
options,
Expand Down Expand Up @@ -197,3 +207,25 @@ function all(parent) {
? results
: results.slice(start, end)
}

/**
* @this {State}
* Info passed around about the current state.
* @param {string | null | undefined} url
* Possible URL value.
* @returns {string}
* URL, resolved to a `base` element, if any.
*/
export function resolve(url) {
const base = this.frozenBaseUrl

if (url === null || url === undefined) {
return ''
}

if (base) {
return String(new URL(url, base))
}

return url
}
23 changes: 0 additions & 23 deletions lib/util/resolve.js

This file was deleted.

0 comments on commit cbee331

Please sign in to comment.