mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
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
This commit is contained in:
@@ -34,11 +34,19 @@ const exts = _.keys(highlights);
|
|||||||
/**
|
/**
|
||||||
* Converts Lodash method references into documentation links.
|
* Converts Lodash method references into documentation links.
|
||||||
*
|
*
|
||||||
|
* Searches for inline code references matching `_.word` (e.g., `_.map`, `_.filter`)
|
||||||
|
* in documentation body text and wraps them in anchor links. Excludes code within
|
||||||
|
* headers as those already have proper anchors.
|
||||||
|
*
|
||||||
* @private
|
* @private
|
||||||
* @param {Object} $ The Cheerio object.
|
* @param {Object} $ The Cheerio object.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* // Body text: <code>_.map</code> → <a href="#map"><code>_.map</code></a>
|
||||||
|
* // Headers: <h3><code>_.VERSION</code></h3> → unchanged (excluded)
|
||||||
*/
|
*/
|
||||||
function autoLink($) {
|
function autoLink($) {
|
||||||
$('.doc-container code').each(function() {
|
$('.doc-container code:not(:header code)').each(function() {
|
||||||
const $code = $(this);
|
const $code = $(this);
|
||||||
const html = $code.html();
|
const html = $code.html();
|
||||||
if (/^_\.\w+$/.test(html)) {
|
if (/^_\.\w+$/.test(html)) {
|
||||||
@@ -176,9 +184,16 @@ function build() {
|
|||||||
.readFileSync(readmePath, 'utf8')
|
.readFileSync(readmePath, 'utf8')
|
||||||
// Uncomment docdown HTML hints.
|
// Uncomment docdown HTML hints.
|
||||||
.replace(/(<)!--\s*|\s*--(>)/g, '$1$2')
|
.replace(/(<)!--\s*|\s*--(>)/g, '$1$2')
|
||||||
// Convert source and npm package links to anchors.
|
// Convert docdown-generated [source] and [npm package] links to HTML.
|
||||||
.replace(/\[source\]\(([^)]+)\) \[npm package\]\(([^)]+)\)/g, (match, href1, href2) =>
|
// These appear as markdown immediately after h3 tags, which marky-markdown
|
||||||
`<p><a href="${ href1 }">source</a> <a href="${ href2 }">npm package</a></p>`
|
// doesn't process (it treats content after HTML blocks as plain text).
|
||||||
|
// Pattern 1: Dual links for methods with npm packages.
|
||||||
|
.replace(/(<h3[^>]*>.*?<\/h3>)\n\[source\]\(([^)]+)\) \[npm package\]\(([^)]+)\)/g, (match, h3, href1, href2) =>
|
||||||
|
`${h3}\n<p><a href="${ href1 }">source</a> <a href="${ href2 }">npm package</a></p>`
|
||||||
|
)
|
||||||
|
// Pattern 2: Standalone [source] links for properties without npm packages.
|
||||||
|
.replace(/(<h3[^>]*>.*?<\/h3>)\n\[source\]\(([^)]+)\)(?! \[npm package\])/g, (match, h3, href) =>
|
||||||
|
`${h3}\n<p><a href="${ href }">source</a></p>`
|
||||||
);
|
);
|
||||||
|
|
||||||
const $ = cheerio.load(marky(markdown, {
|
const $ = cheerio.load(marky(markdown, {
|
||||||
|
|||||||
Reference in New Issue
Block a user