-
-
Notifications
You must be signed in to change notification settings - Fork 82
Added types for url-utils #685
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
Changes from 10 commits
dd62bac
7b4e571
5795a7a
fcc7436
6361779
18d88a9
615872c
7549bac
9d3d43c
5fbb27f
683e18c
201280b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import UrlUtils from './UrlUtils'; | ||
|
|
||
| export = UrlUtils; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| // @ts-nocheck | ||
| function escapeRegExp(string) { | ||
| import type {BaseUrlOptionsInput} from './types'; | ||
|
|
||
| function escapeRegExp(string: string): string { | ||
| return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| type BuildEarlyExitMatchOptions = BaseUrlOptionsInput & { | ||
| ignoreProtocol?: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Build a regex pattern that matches any of the configured base URLs (site URL + CDN URLs). | ||
| * This is used for early exit optimizations - if content doesn't contain any of these URLs, | ||
|
|
@@ -16,14 +21,14 @@ function escapeRegExp(string) { | |
| * @param {boolean} [options.ignoreProtocol=true] - Whether to strip protocol from URLs | ||
| * @returns {string|null} Regex pattern matching any configured base URL, or null if none configured | ||
| */ | ||
| function buildEarlyExitMatch(siteUrl, options = {}) { | ||
| function buildEarlyExitMatch(siteUrl: string, options: BuildEarlyExitMatchOptions = {}): string | null { | ||
| const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl] | ||
| .filter(Boolean) | ||
| .map((value) => { | ||
| .filter((value): value is string => Boolean(value)) | ||
|
||
| .map((value: string) => { | ||
| let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value; | ||
| return normalized.replace(/\/$/, ''); | ||
| }) | ||
| .filter(Boolean) | ||
| .filter((value): value is string => Boolean(value)) | ||
|
||
| .map(escapeRegExp); | ||
|
|
||
| if (!candidates.length) { | ||
|
|
@@ -37,7 +42,7 @@ function buildEarlyExitMatch(siteUrl, options = {}) { | |
| return `(?:${candidates.join('|')})`; | ||
| } | ||
|
|
||
| module.exports = { | ||
| export default { | ||
| buildEarlyExitMatch, | ||
| escapeRegExp | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| // @ts-nocheck | ||
| function deduplicateDoubleSlashes(url) { | ||
| function deduplicateDoubleSlashes(url: string): string { | ||
| // Preserve protocol slashes (e.g., http://, https://) and only deduplicate | ||
| // slashes in the path portion. The pattern (^|[^:])\/\/+ matches double slashes | ||
| // that are either at the start of the string or not preceded by a colon. | ||
| return url.replace(/(^|[^:])\/\/+/g, '$1/'); | ||
| } | ||
|
|
||
| module.exports = deduplicateDoubleSlashes; | ||
| export default deduplicateDoubleSlashes; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,26 @@ | ||
| // @ts-nocheck | ||
| const htmlTransform = require('./html-transform'); | ||
| const absoluteToRelative = require('./absolute-to-relative'); | ||
| import type {HtmlTransformOptionsInput} from './types'; | ||
| import type {AbsoluteToRelativeOptionsInput} from './absolute-to-relative'; | ||
| import htmlTransform from './html-transform'; | ||
| import absoluteToRelative from './absolute-to-relative'; | ||
|
|
||
| function htmlAbsoluteToRelative(html = '', siteUrl, _options) { | ||
| const defaultOptions = {assetsOnly: false, ignoreProtocol: true}; | ||
| const options = Object.assign({}, defaultOptions, _options || {}); | ||
| function htmlAbsoluteToRelative( | ||
| html: string = '', | ||
| siteUrl: string, | ||
| _options: AbsoluteToRelativeOptionsInput = {} | ||
| ): string { | ||
| const defaultOptions: AbsoluteToRelativeOptionsInput = {assetsOnly: false, ignoreProtocol: true}; | ||
| const options: HtmlTransformOptionsInput = Object.assign({}, defaultOptions, _options || {}); | ||
|
|
||
| // exit early and avoid parsing if the content does not contain the siteUrl | ||
| options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl; | ||
| options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, ''); | ||
|
|
||
| // need to ignore itemPath because absoluteToRelative doesn't take that option | ||
| const transformFunction = function (_url, _siteUrl, _itemPath, __options) { | ||
| const transformFunction = function (_url: string, _siteUrl: string, _itemPath: string | null, __options: AbsoluteToRelativeOptionsInput): string { | ||
| return absoluteToRelative(_url, _siteUrl, __options); | ||
| }; | ||
|
|
||
| return htmlTransform(html, siteUrl, transformFunction, '', options); | ||
| } | ||
|
|
||
| module.exports = htmlAbsoluteToRelative; | ||
| export default htmlAbsoluteToRelative; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,29 @@ | ||
| // @ts-nocheck | ||
| const htmlTransform = require('./html-transform'); | ||
| const absoluteToTransformReady = require('./absolute-to-transform-ready'); | ||
| const {buildEarlyExitMatch} = require('./build-early-exit-match'); | ||
| import type {HtmlTransformOptionsInput, AbsoluteToTransformReadyOptionsInput} from './types'; | ||
| import htmlTransform from './html-transform'; | ||
| import absoluteToTransformReady from './absolute-to-transform-ready'; | ||
| import buildEarlyExitMatchModule from './build-early-exit-match'; | ||
| const {buildEarlyExitMatch} = buildEarlyExitMatchModule; | ||
|
|
||
| const htmlAbsoluteToTransformReady = function (html = '', siteUrl, _options) { | ||
| const defaultOptions = {assetsOnly: false, ignoreProtocol: true}; | ||
| const options = Object.assign({}, defaultOptions, _options || {}); | ||
| const htmlAbsoluteToTransformReady = function ( | ||
| html: string = '', | ||
| siteUrl: string, | ||
| _options: AbsoluteToTransformReadyOptionsInput = {} | ||
| ): string { | ||
| const defaultOptions: AbsoluteToTransformReadyOptionsInput = {assetsOnly: false, ignoreProtocol: true}; | ||
| const options: HtmlTransformOptionsInput = Object.assign({}, defaultOptions, _options || {}); | ||
|
|
||
| // exit early and avoid parsing if the content does not contain the siteUrl or configured asset bases | ||
| options.earlyExitMatchStr = buildEarlyExitMatch(siteUrl, options); | ||
| const earlyExitMatch = buildEarlyExitMatch(siteUrl, options); | ||
| if (earlyExitMatch) { | ||
| options.earlyExitMatchStr = earlyExitMatch; | ||
| } | ||
|
|
||
| // need to ignore itemPath because absoluteToRelative doesn't take that option | ||
| const transformFunction = function (_url, _siteUrl, _itemPath, __options) { | ||
| const transformFunction = function (_url: string, _siteUrl: string, _itemPath: string | null, __options: AbsoluteToTransformReadyOptionsInput): string { | ||
| return absoluteToTransformReady(_url, _siteUrl, __options); | ||
| }; | ||
|
|
||
| return htmlTransform(html, siteUrl, transformFunction, '', options); | ||
| }; | ||
|
|
||
| module.exports = htmlAbsoluteToTransformReady; | ||
| export default htmlAbsoluteToTransformReady; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to type cast here? Can we instead make sure that
optionsis of typeAbsoluteToTransformReadyOptionsand then it overlaps?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I've updated
AbsoluteToTransformReadyOptionsto extendAbsoluteToRelativeOptions, which makes the types properly overlap. Removed type casts.