From 53ba488c8f1b722f3f099f84b61a9d2a5850021e Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sat, 1 Apr 2023 12:02:33 +0200 Subject: [PATCH 1/2] chore(docs/source/api): properly recognize "async function" in dox --- docs/source/api.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/docs/source/api.js b/docs/source/api.js index 4eab51320fe..de6bfe2e0fe 100644 --- a/docs/source/api.js +++ b/docs/source/api.js @@ -41,6 +41,53 @@ const files = [ const out = module.exports.docs = []; +// add custom matchers to dox, to recognize things it does not know about +// see https://github.com/tj/dox/issues/198 +{ + // Some matchers need to be in a specific order, like the "prototype" matcher must be before the static matcher (and inverted because "unshift") + + // "unshift" is used, because the first function to return a object from "contextPatternMatchers" is used (and we need to "overwrite" those specific functions) + + // push a matcher to recognize "Class.fn = async function" as a method + dox.contextPatternMatchers.unshift(function(str) { + const match = /^\s*([\w$.]+)\s*\.\s*([\w$]+)\s*=\s*(?:async\s+)?function/.exec(str); + if (match) { + return { + type: 'method', + receiver: match[1], + name: match[2], + string: match[1] + '.' + match[2] + '()' + }; + } + }); + + // push a matcher to recognize "Class.prototype.fn = async function" as a method + dox.contextPatternMatchers.unshift(function(str) { + const match = /^\s*([\w$.]+)\s*\.\s*prototype\s*\.\s*([\w$]+)\s*=\s*(?:async\s+)?function/.exec(str); + if (match) { + return { + type: 'method', + constructor: match[1], + cons: match[1], + name: match[2], + string: match[1] + '.prototype.' + match[2] + '()' + }; + } + }); + + // push a matcher to recognize "async function" as a function + dox.contextPatternMatchers.unshift(function(str) { + const match = /^\s*(export(\s+default)?\s+)?(?:async\s+)?function\s+([\w$]+)\s*\(/.exec(str); + if (match) { + return { + type: 'function', + name: match[3], + string: match[3] + '()' + }; + } + }); +} + const combinedFiles = []; for (const file of files) { try { From 19cc0965663a7f45df8139abd55ccc7ec41cfcc6 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Sat, 1 Apr 2023 12:08:19 +0200 Subject: [PATCH 2/2] docs(js/convert-old-anchorid): add convertion for improper async fn anchors --- docs/js/convert-old-anchorid.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/js/convert-old-anchorid.js b/docs/js/convert-old-anchorid.js index fd8054019c2..5f651092f6e 100644 --- a/docs/js/convert-old-anchorid.js +++ b/docs/js/convert-old-anchorid.js @@ -5,7 +5,7 @@ window.addEventListener('DOMContentLoaded', () => { // only operate on the old id's if (!/^#\w+_\w+(?:-\w+)?$/i.test(anchor)) { - return; + return fixNoAsyncFn(); } // in case there is no anchor, return without modifying the anchor @@ -68,4 +68,25 @@ window.addEventListener('DOMContentLoaded', () => { window.location.hash = `#${test}`; } } + + // function to fix dox not recognizing async functions and resulting in inproper anchors + function fixNoAsyncFn() { + const anchorSlice = anchor.slice(1); + // dont modify anchor if it already exists + if (document.querySelector(`h3[id="${anchorSlice}"`)) { + return; + } + + const tests = [ + `${anchorSlice}()` + ]; + + for (const test of tests) { + // have to use the "[id=]" selector because "#Something()" is not a valid selector (the "()" part) + const header = document.querySelector(`h3[id="${test}"]`); + if (header) { + window.location.hash = `#${test}`; + } + } + } }, { once: true });