@@ -228,9 +228,15 @@ function xref_unresolved(node)
228
228
occursin (XREF_REGEX, node. element. destination)
229
229
end
230
230
231
-
232
231
function Selectors. matcher (:: Type{XRefResolvers.Header} , node, slug, meta, page, doc, errors)
233
- return (xref_unresolved (node) && anchor_exists (doc. internal. headers, slug))
232
+ xref_unresolved (node) || return false
233
+ dest = xrefname (node. element. destination)
234
+
235
+ if isempty (dest)
236
+ return first (linkcontent (node)) ∈ (:text , :complex )
237
+ else
238
+ return ! startswith (dest, " #" ) && occursin (HEADER_REGEX, dest)
239
+ end
234
240
end
235
241
236
242
function Selectors. runner (:: Type{XRefResolvers.Header} , node, slug, meta, page, doc, errors)
248
254
249
255
250
256
function Selectors. matcher (:: Type{XRefResolvers.Docs} , node, slug, meta, page, doc, errors)
251
- return xref_unresolved (node)
257
+ xref_unresolved (node) || return false
258
+
259
+ dest = xrefname (node. element. destination)
260
+
261
+ if isempty (dest)
262
+ return first (linkcontent (node)) == :code
263
+ else
264
+ return ! startswith (dest, " #" )
265
+ end
252
266
end
253
267
254
268
function Selectors. runner (:: Type{XRefResolvers.Docs} , node, slug, meta, page, doc, errors)
@@ -285,15 +299,7 @@ function xref(node::MarkdownAST.Node, meta, page, doc)
285
299
slug = xrefname (link. destination)
286
300
@assert ! isnothing (slug)
287
301
if isempty (slug)
288
- # obtain a slug from the link text
289
- if length (node. children) == 1 && isa (first (node. children). element, MarkdownAST. Code)
290
- slug = first (node. children). element. code
291
- else
292
- # TODO : remove this hack (replace with mdflatten?)
293
- md = _link_node_as_md (node)
294
- text = strip (sprint (Markdown. plain, Markdown. Paragraph (md. content[1 ]. content[1 ]. text)))
295
- slug = Documenter. slugify (text)
296
- end
302
+ slug = Documenter. slugify (last (linkcontent (node)))
297
303
else
298
304
# explicit slugs that are enclosed in quotes must be further sluggified
299
305
stringmatch = match (r" \" (.+)\" " , slug)
@@ -341,6 +347,22 @@ function xrefname(link_url::AbstractString)
341
347
return isnothing (m[1 ]) ? " " : strip (m[1 ])
342
348
end
343
349
350
+ function linkcontent (node:: MarkdownAST.Node )
351
+ isa (node. element, MarkdownAST. Link) || return nothing
352
+
353
+ if length (node. children) == 1
354
+ child = first (node. children). element
355
+ if isa (child, MarkdownAST. Code)
356
+ return (:code , child. code)
357
+ elseif isa (child, MarkdownAST. Text)
358
+ return (:text , child. text)
359
+ end
360
+ end
361
+
362
+ text = MDFlatten. mdflatten (node)
363
+ return (:complex , text)
364
+ end
365
+
344
366
""" Regular expression for an `@ref` link url.
345
367
346
368
This is used by the [`XRefResolvers.XRefResolverPipeline`](@ref), respectively
@@ -350,28 +372,34 @@ pipeline.
350
372
"""
351
373
const XREF_REGEX = r" ^\s *@ref(\s .*)?$"
352
374
375
+ """ Regular expression for a slug
376
+ """
377
+ const HEADER_REGEX = r" ^\" .+\" $"
378
+
353
379
354
380
# Cross referencing headers.
355
381
# --------------------------
356
382
357
383
function namedxref (node:: MarkdownAST.Node , slug, meta, page, doc, errors)
358
384
@assert node. element isa MarkdownAST. Link
359
385
headers = doc. internal. headers
360
- @assert anchor_exists (headers, slug)
361
- # Add the link to list of local uncheck links.
362
- doc. internal. locallinks[node. element] = node. element. destination
363
- # Error checking: `slug` should exist and be unique.
364
- # TODO : handle non-unique slugs.
365
- if anchor_isunique (headers, slug)
366
- # Replace the `@ref` url with a path to the referenced header.
367
- anchor = Documenter. anchor (headers, slug)
368
- pagekey = relpath (anchor. file, doc. user. build)
369
- page = doc. blueprint. pages[pagekey]
370
- node. element = Documenter. PageLink (page, anchor_label (anchor))
386
+ if anchor_exists (headers, slug)
387
+ # Add the link to list of local uncheck links.
388
+ doc. internal. locallinks[node. element] = node. element. destination
389
+ # Error checking: `slug` should exist and be unique.
390
+ # TODO : handle non-unique slugs.
391
+ if anchor_isunique (headers, slug)
392
+ # Replace the `@ref` url with a path to the referenced header.
393
+ anchor = Documenter. anchor (headers, slug)
394
+ pagekey = relpath (anchor. file, doc. user. build)
395
+ page = doc. blueprint. pages[pagekey]
396
+ node. element = Documenter. PageLink (page, anchor_label (anchor))
397
+ else
398
+ push! (errors, " Header with slug '$slug ' is not unique in $(Documenter. locrepr (page. source)) ." )
399
+ end
371
400
else
372
- push! (errors, " Header with slug '$slug ' is not unique in $(Documenter. locrepr (page. source)) ." )
401
+ push! (errors, " Header with slug '$slug ' in $(Documenter. locrepr (page. source)) does not exist ." )
373
402
end
374
- return
375
403
end
376
404
377
405
# Cross referencing docstrings.
0 commit comments