Skip to content

Commit 8f96d3f

Browse files
committed
chore: fix url.parse deprecation warning on Node 24+
## Motivation Fixes the url.parse deprecation warning that appears when running on Node 24 or later (see [DEP0169](https://nodejs.org/docs/latest-v24.x/api/deprecations.html#DEP0169)).
1 parent 05acc90 commit 8f96d3f

2 files changed

Lines changed: 9 additions & 23 deletions

File tree

  • packages/docusaurus-mdx-loader/src/remark

packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
import path from 'path';
9-
import url from 'url';
109
import fs from 'fs-extra';
1110
import {
1211
toMessageRelativeFilePath,
@@ -207,27 +206,18 @@ async function processImageNode(target: Target, context: Context) {
207206
return;
208207
}
209208

210-
const parsedUrl = url.parse(node.url);
211-
if (parsedUrl.protocol || !parsedUrl.pathname) {
209+
if (URL.canParse(node.url)) {
212210
// pathname:// is an escape hatch, in case user does not want her images to
213211
// be converted to require calls going through webpack loader
214-
if (parsedUrl.protocol === 'pathname:') {
212+
if (parseURLOrPath(node.url).protocol === 'pathname:') {
215213
node.url = node.url.replace('pathname://', '');
216214
}
217215
return;
218216
}
219217

220-
// We decode it first because Node Url.pathname is always encoded
221-
// while the image file-system path are not.
222-
// See https://github.com/facebook/docusaurus/discussions/10720
223-
const decodedPathname = decodeURIComponent(parsedUrl.pathname);
224-
225218
// We try to convert image urls without protocol to images with require calls
226219
// going through webpack ensures that image assets exist at build time
227-
const localImagePath = await getLocalImageAbsolutePath(
228-
decodedPathname,
229-
context,
230-
);
220+
const localImagePath = await getLocalImageAbsolutePath(node.url, context);
231221
if (localImagePath === null) {
232222
node.url =
233223
context.onBrokenMarkdownImages({

packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
import path from 'path';
9-
import url from 'url';
109
import fs from 'fs-extra';
1110
import {
1211
toMessageRelativeFilePath,
@@ -209,23 +208,20 @@ async function processLinkNode(target: Target, context: Context) {
209208
return;
210209
}
211210

212-
const parsedUrl = url.parse(node.url);
213-
if (parsedUrl.protocol || !parsedUrl.pathname) {
211+
const parsedUrl = parseURLOrPath(node.url);
212+
if (URL.canParse(node.url) || parsedUrl.protocol === 'pathname:') {
214213
// Don't process pathname:// here, it's used by the <Link> component
215214
return;
216215
}
217-
const hasSiteAlias = parsedUrl.pathname.startsWith('@site/');
216+
217+
const hasSiteAlias = node.url.startsWith('@site/');
218218
const hasAssetLikeExtension =
219-
path.extname(parsedUrl.pathname) &&
220-
!parsedUrl.pathname.match(/\.(?:mdx?|html)(?:#|$)/);
219+
path.extname(node.url) && !node.url.match(/\.(?:mdx?|html)(?:#|$)/);
221220
if (!hasSiteAlias && !hasAssetLikeExtension) {
222221
return;
223222
}
224223

225-
const localFilePath = await getLocalFileAbsolutePath(
226-
decodeURIComponent(parsedUrl.pathname),
227-
context,
228-
);
224+
const localFilePath = await getLocalFileAbsolutePath(node.url, context);
229225

230226
if (localFilePath) {
231227
await toAssetRequireNode(target, localFilePath, context);

0 commit comments

Comments
 (0)