Skip to content

Commit 4879a7a

Browse files
authored
doc: fix autoLink function, conversion of source links (#6056)
* doc: fix autoLink function, dont autolink from headers * doc: fix conversion of source only links from md to html
1 parent 9648f69 commit 4879a7a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lib/main/build-site.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ const exts = _.keys(highlights);
3434
/**
3535
* Converts Lodash method references into documentation links.
3636
*
37+
* Searches for inline code references matching `_.word` (e.g., `_.map`, `_.filter`)
38+
* in documentation body text and wraps them in anchor links. Excludes code within
39+
* headers as those already have proper anchors.
40+
*
3741
* @private
3842
* @param {Object} $ The Cheerio object.
43+
*
44+
* @example
45+
* // Body text: <code>_.map</code> → <a href="#map"><code>_.map</code></a>
46+
* // Headers: <h3><code>_.VERSION</code></h3> → unchanged (excluded)
3947
*/
4048
function autoLink($) {
41-
$('.doc-container code').each(function() {
49+
$('.doc-container code:not(:header code)').each(function() {
4250
const $code = $(this);
4351
const html = $code.html();
4452
if (/^_\.\w+$/.test(html)) {
@@ -176,9 +184,16 @@ function build() {
176184
.readFileSync(readmePath, 'utf8')
177185
// Uncomment docdown HTML hints.
178186
.replace(/(<)!--\s*|\s*--(>)/g, '$1$2')
179-
// Convert source and npm package links to anchors.
180-
.replace(/\[source\]\(([^)]+)\) \[npm package\]\(([^)]+)\)/g, (match, href1, href2) =>
181-
`<p><a href="${ href1 }">source</a> <a href="${ href2 }">npm package</a></p>`
187+
// Convert docdown-generated [source] and [npm package] links to HTML.
188+
// These appear as markdown immediately after h3 tags, which marky-markdown
189+
// doesn't process (it treats content after HTML blocks as plain text).
190+
// Pattern 1: Dual links for methods with npm packages.
191+
.replace(/(<h3[^>]*>.*?<\/h3>)\n\[source\]\(([^)]+)\) \[npm package\]\(([^)]+)\)/g, (match, h3, href1, href2) =>
192+
`${h3}\n<p><a href="${ href1 }">source</a> <a href="${ href2 }">npm package</a></p>`
193+
)
194+
// Pattern 2: Standalone [source] links for properties without npm packages.
195+
.replace(/(<h3[^>]*>.*?<\/h3>)\n\[source\]\(([^)]+)\)(?! \[npm package\])/g, (match, h3, href) =>
196+
`${h3}\n<p><a href="${ href }">source</a></p>`
182197
);
183198

184199
const $ = cheerio.load(marky(markdown, {

0 commit comments

Comments
 (0)