Skip to content

Commit 683e18c

Browse files
committed
Fix TypeScript migration issues based on PR feedback
- replace-permalink: Return 'undefined' for missing author (match original behavior) - replace-permalink: Remove unnecessary String() conversion for id - absolute-to-transform-ready: Extend AbsoluteToRelativeOptions to remove type casts - build-early-exit-match: Fix type guards to properly check string type and length - markdown-absolute-to-relative: Remove unnecessary earlyExitMatchStr check - relative-to-absolute: Remove incorrect comment and unsafe ! operator, use type assertion
1 parent 5fbb27f commit 683e18c

File tree

5 files changed

+12
-18
lines changed

5 files changed

+12
-18
lines changed

packages/url-utils/src/utils/absolute-to-transform-ready.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import type {TransformReadyReplacementOptions} from './types';
2-
import absoluteToRelative, {type AbsoluteToRelativeOptionsInput} from './absolute-to-relative';
2+
import absoluteToRelative, {type AbsoluteToRelativeOptions} from './absolute-to-relative';
33
import {URL} from 'url';
44

5-
export interface AbsoluteToTransformReadyOptions extends TransformReadyReplacementOptions {
5+
export interface AbsoluteToTransformReadyOptions extends TransformReadyReplacementOptions, AbsoluteToRelativeOptions {
66
withoutSubdirectory: boolean;
7-
ignoreProtocol: boolean;
8-
assetsOnly: boolean;
9-
staticImageUrlPrefix: string;
107
staticFilesUrlPrefix?: string;
118
staticMediaUrlPrefix?: string;
129
imageBaseUrl?: string | null;
@@ -53,30 +50,30 @@ const absoluteToTransformReady = function (
5350

5451
// convert to relative with stripped subdir
5552
// always returns root-relative starting with forward slash
56-
const rootRelativeUrl = absoluteToRelative(url, root, options as AbsoluteToRelativeOptionsInput);
53+
const rootRelativeUrl = absoluteToRelative(url, root, options);
5754

5855
if (isRelative(rootRelativeUrl)) {
5956
return `${options.replacementStr}${rootRelativeUrl}`;
6057
}
6158

6259
if (options.mediaBaseUrl) {
63-
const mediaRelativeUrl = absoluteToRelative(url, options.mediaBaseUrl, options as AbsoluteToRelativeOptionsInput);
60+
const mediaRelativeUrl = absoluteToRelative(url, options.mediaBaseUrl, options);
6461

6562
if (isRelative(mediaRelativeUrl)) {
6663
return `${options.replacementStr}${mediaRelativeUrl}`;
6764
}
6865
}
6966

7067
if (options.filesBaseUrl) {
71-
const filesRelativeUrl = absoluteToRelative(url, options.filesBaseUrl, options as AbsoluteToRelativeOptionsInput);
68+
const filesRelativeUrl = absoluteToRelative(url, options.filesBaseUrl, options);
7269

7370
if (isRelative(filesRelativeUrl)) {
7471
return `${options.replacementStr}${filesRelativeUrl}`;
7572
}
7673
}
7774

7875
if (options.imageBaseUrl) {
79-
const imageRelativeUrl = absoluteToRelative(url, options.imageBaseUrl, options as AbsoluteToRelativeOptionsInput);
76+
const imageRelativeUrl = absoluteToRelative(url, options.imageBaseUrl, options);
8077

8178
if (isRelative(imageRelativeUrl)) {
8279
return `${options.replacementStr}${imageRelativeUrl}`;

packages/url-utils/src/utils/build-early-exit-match.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ type BuildEarlyExitMatchOptions = BaseUrlOptionsInput & {
2323
*/
2424
function buildEarlyExitMatch(siteUrl: string, options: BuildEarlyExitMatchOptions = {}): string | null {
2525
const candidates = [siteUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
26-
.filter((value): value is string => Boolean(value))
26+
.filter((value: string | null | undefined): value is string => typeof value === 'string' && value.length > 0)
2727
.map((value: string) => {
2828
let normalized = options.ignoreProtocol ? value.replace(/http:|https:/, '') : value;
2929
return normalized.replace(/\/$/, '');
3030
})
31-
.filter((value): value is string => Boolean(value))
31+
.filter((value: string): boolean => Boolean(value))
3232
.map(escapeRegExp);
3333

3434
if (!candidates.length) {

packages/url-utils/src/utils/markdown-absolute-to-relative.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ function markdownAbsoluteToRelative(
1313
const options: MarkdownTransformOptionsInput = Object.assign({}, defaultOptions, _options);
1414

1515
options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl;
16-
if (options.earlyExitMatchStr) {
17-
options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
18-
}
16+
options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
1917

2018
// need to ignore itemPath because absoluteToRelative functions doen't take that option
2119
const transformFunctions = {

packages/url-utils/src/utils/relative-to-absolute.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ const relativeToAbsolute = function relativeToAbsolute(
5555

5656
// return the path as-is if it's not an asset path and we're only modifying assets
5757
if (options.assetsOnly) {
58-
// staticImageUrlPrefix is always set from defaultOptions via Object.assign
59-
const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix!);
58+
const staticImageUrlPrefixRegex = new RegExp(options.staticImageUrlPrefix as string);
6059
if (!path.match(staticImageUrlPrefixRegex)) {
6160
return path;
6261
}

packages/url-utils/src/utils/replace-permalink.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function replacePermalink(permalink: string, resource: PermalinkResource, timezo
2929
return publishedAtMoment.format('DD');
3030
},
3131
author: function () {
32-
return resource.primary_author?.slug ?? primaryTagFallback;
32+
return resource.primary_author?.slug ?? 'undefined';
3333
},
3434
primary_author: function () {
3535
return resource.primary_author ? resource.primary_author.slug : primaryTagFallback;
@@ -41,7 +41,7 @@ function replacePermalink(permalink: string, resource: PermalinkResource, timezo
4141
return resource.slug;
4242
},
4343
id: function () {
44-
return String(resource.id);
44+
return resource.id;
4545
}
4646
};
4747

0 commit comments

Comments
 (0)