@diplodoc/transform 4.50.0 → 4.50.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -144,19 +144,19 @@ function processLink(
144
144
  const linkToken = tokens[idx];
145
145
  const nextToken = tokens[idx + 1];
146
146
 
147
- let href = getHrefTokenAttr(linkToken);
147
+ const originalHref = getHrefTokenAttr(linkToken);
148
148
 
149
- if (!href) {
149
+ if (!originalHref) {
150
150
  log.error(`Empty link in ${bold(startPath)}`);
151
151
  return;
152
152
  }
153
153
 
154
- const {pathname, hash} = url.parse(href);
154
+ const {pathname, hash} = url.parse(originalHref);
155
155
  let file;
156
156
  let fileExists;
157
157
  let isPageFile;
158
158
 
159
- if (!isLocalUrl(href)) {
159
+ if (!isLocalUrl(originalHref)) {
160
160
  linkToken.attrSet('target', '_blank');
161
161
  linkToken.attrSet('rel', 'noreferrer noopener');
162
162
  return;
@@ -170,7 +170,7 @@ function processLink(
170
170
  if (isPageFile && !fileExists) {
171
171
  let needShowError = true;
172
172
  if (needSkipLinkFn) {
173
- needShowError = !needSkipLinkFn(href);
173
+ needShowError = !needSkipLinkFn(originalHref);
174
174
  }
175
175
 
176
176
  if (notFoundCb && needShowError) {
@@ -178,7 +178,7 @@ function processLink(
178
178
  }
179
179
 
180
180
  if (needShowError) {
181
- log.error(`Link is unreachable: ${bold(href)} in ${bold(currentPath)}`);
181
+ log.error(`Link is unreachable: ${bold(originalHref)} in ${bold(currentPath)}`);
182
182
  }
183
183
  }
184
184
  } else if (hash) {
@@ -206,28 +206,23 @@ function processLink(
206
206
  tokens,
207
207
  idx,
208
208
  nextToken,
209
- href,
209
+ href: originalHref,
210
210
  currentPath,
211
211
  log,
212
212
  cache,
213
213
  });
214
214
  }
215
215
 
216
- let newPathname = '';
216
+ const patchedHref =
217
+ !isAbsolute(originalHref) && !originalHref.includes('//')
218
+ ? url.format({
219
+ ...url.parse(originalHref),
220
+ pathname: getPublicPath(opts, file),
221
+ })
222
+ : originalHref;
223
+ const linkHrefTransformer = transformLink || defaultTransformLink;
217
224
 
218
- if (!isAbsolute(href) && !href.includes('//')) {
219
- newPathname = getPublicPath(opts, file);
220
-
221
- href = url.format({
222
- ...url.parse(href),
223
- pathname: newPathname,
224
- });
225
- }
226
-
227
- if (pathname || newPathname) {
228
- const transformer = transformLink || defaultTransformLink;
229
- linkToken.attrSet('href', transformer(href));
230
- }
225
+ linkToken.attrSet('href', linkHrefTransformer(patchedHref));
231
226
  }
232
227
 
233
228
  const index: MarkdownItPluginCb<ProcOpts & Options> = (md: MarkdownItIncluded, opts) => {
@@ -241,14 +236,20 @@ const index: MarkdownItPluginCb<ProcOpts & Options> = (md: MarkdownItIncluded, o
241
236
  let j = 0;
242
237
 
243
238
  while (j < childrenTokens.length) {
244
- const isLinkOpenToken = childrenTokens[j].type === 'link_open';
245
- const tokenClass = childrenTokens[j].attrGet('class');
239
+ const token = childrenTokens[j];
240
+
241
+ const isLinkOpenToken = token.type === 'link_open';
242
+ const tokenClass = token.attrGet('class');
246
243
 
247
244
  /* Don't process anchor links */
248
245
  const isYfmAnchor = tokenClass ? tokenClass.includes('yfm-anchor') : false;
246
+ const wasProcessedBefore = Boolean(token.meta?.yfmLinkPluginProcessed);
249
247
 
250
- if (isLinkOpenToken && !isYfmAnchor) {
248
+ if (isLinkOpenToken && !wasProcessedBefore && !isYfmAnchor) {
251
249
  processLink(md, state, childrenTokens, j, opts);
250
+
251
+ token.meta ??= {};
252
+ token.meta.yfmLinkPluginProcessed = true;
252
253
  }
253
254
 
254
255
  j++;