@mintlify/scraping 3.0.186 → 3.0.188
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.
- package/bin/assert.d.ts +5 -0
- package/bin/assert.js +13 -0
- package/bin/assert.js.map +1 -0
- package/bin/cli.js +43 -72
- package/bin/cli.js.map +1 -1
- package/bin/components/Accordion.d.ts +5 -0
- package/bin/components/Accordion.js +54 -0
- package/bin/components/Accordion.js.map +1 -0
- package/bin/components/AccordionGroup.d.ts +5 -0
- package/bin/components/AccordionGroup.js +52 -0
- package/bin/components/AccordionGroup.js.map +1 -0
- package/bin/components/Callout.d.ts +5 -0
- package/bin/components/Callout.js +114 -0
- package/bin/components/Callout.js.map +1 -0
- package/bin/components/Card.d.ts +5 -0
- package/bin/components/Card.js +135 -0
- package/bin/components/Card.js.map +1 -0
- package/bin/components/CardGroup.d.ts +5 -0
- package/bin/components/CardGroup.js +52 -0
- package/bin/components/CardGroup.js.map +1 -0
- package/bin/components/CodeGroup.d.ts +5 -0
- package/bin/components/CodeGroup.js +166 -0
- package/bin/components/CodeGroup.js.map +1 -0
- package/bin/components/Frame.d.ts +5 -0
- package/bin/components/Frame.js +51 -0
- package/bin/components/Frame.js.map +1 -0
- package/bin/components/Tabs.d.ts +5 -0
- package/bin/components/Tabs.js +122 -0
- package/bin/components/Tabs.js.map +1 -0
- package/bin/components/link.d.ts +2 -0
- package/bin/components/link.js +16 -0
- package/bin/components/link.js.map +1 -0
- package/bin/constants.d.ts +6 -7
- package/bin/constants.js +31 -12
- package/bin/constants.js.map +1 -1
- package/bin/customComponents/create.d.ts +10 -0
- package/bin/customComponents/create.js +69 -0
- package/bin/customComponents/create.js.map +1 -0
- package/bin/customComponents/plugin.d.ts +2 -0
- package/bin/customComponents/plugin.js +26 -0
- package/bin/customComponents/plugin.js.map +1 -0
- package/bin/customComponents/selective.d.ts +6 -0
- package/bin/customComponents/selective.js +29 -0
- package/bin/customComponents/selective.js.map +1 -0
- package/bin/nav/iterate.d.ts +2 -0
- package/bin/nav/iterate.js +15 -0
- package/bin/nav/iterate.js.map +1 -0
- package/bin/nav/listItems.d.ts +8 -0
- package/bin/nav/listItems.js +62 -0
- package/bin/nav/listItems.js.map +1 -0
- package/bin/nav/retrieve.d.ts +3 -0
- package/bin/nav/retrieve.js +75 -0
- package/bin/nav/retrieve.js.map +1 -0
- package/bin/nav/root.d.ts +2 -0
- package/bin/nav/root.js +40 -0
- package/bin/nav/root.js.map +1 -0
- package/bin/openapi/generateOpenApiPages.js +18 -5
- package/bin/openapi/generateOpenApiPages.js.map +1 -1
- package/bin/root/retrieve.d.ts +2 -0
- package/bin/root/retrieve.js +46 -0
- package/bin/root/retrieve.js.map +1 -0
- package/bin/scrapingPipeline/group.d.ts +5 -0
- package/bin/scrapingPipeline/group.js +46 -0
- package/bin/scrapingPipeline/group.js.map +1 -0
- package/bin/scrapingPipeline/icon.d.ts +2 -0
- package/bin/scrapingPipeline/icon.js +22 -0
- package/bin/scrapingPipeline/icon.js.map +1 -0
- package/bin/scrapingPipeline/images.d.ts +3 -0
- package/bin/scrapingPipeline/images.js +50 -0
- package/bin/scrapingPipeline/images.js.map +1 -0
- package/bin/scrapingPipeline/logo.d.ts +5 -0
- package/bin/scrapingPipeline/logo.js +92 -0
- package/bin/scrapingPipeline/logo.js.map +1 -0
- package/bin/scrapingPipeline/page.d.ts +6 -0
- package/bin/scrapingPipeline/page.js +102 -0
- package/bin/scrapingPipeline/page.js.map +1 -0
- package/bin/scrapingPipeline/root.d.ts +2 -0
- package/bin/scrapingPipeline/root.js +8 -0
- package/bin/scrapingPipeline/root.js.map +1 -0
- package/bin/scrapingPipeline/site.d.ts +7 -0
- package/bin/scrapingPipeline/site.js +129 -0
- package/bin/scrapingPipeline/site.js.map +1 -0
- package/bin/scrapingPipeline/tabs.d.ts +3 -0
- package/bin/scrapingPipeline/tabs.js +67 -0
- package/bin/scrapingPipeline/tabs.js.map +1 -0
- package/bin/tabs/retrieveReadme.d.ts +3 -0
- package/bin/tabs/retrieveReadme.js +78 -0
- package/bin/tabs/retrieveReadme.js.map +1 -0
- package/bin/tsconfig.build.tsbuildinfo +1 -1
- package/bin/types/components.d.ts +2 -0
- package/bin/types/components.js +2 -0
- package/bin/types/components.js.map +1 -0
- package/bin/types/framework.d.ts +8 -0
- package/bin/types/framework.js +3 -0
- package/bin/types/framework.js.map +1 -0
- package/bin/types/hast.d.ts +6 -0
- package/bin/types/hast.js +2 -0
- package/bin/types/hast.js.map +1 -0
- package/bin/types/result.d.ts +7 -0
- package/bin/types/result.js +2 -0
- package/bin/types/result.js.map +1 -0
- package/bin/types/scrapeFunc.d.ts +3 -0
- package/bin/types/scrapeFunc.js +2 -0
- package/bin/types/scrapeFunc.js.map +1 -0
- package/bin/utils/append.d.ts +1 -0
- package/bin/utils/append.js +12 -0
- package/bin/utils/append.js.map +1 -0
- package/bin/utils/children.d.ts +5 -0
- package/bin/utils/children.js +35 -0
- package/bin/utils/children.js.map +1 -0
- package/bin/utils/className.d.ts +3 -0
- package/bin/utils/className.js +13 -0
- package/bin/utils/className.js.map +1 -0
- package/bin/utils/detectFramework.d.ts +4 -0
- package/bin/utils/detectFramework.js +60 -0
- package/bin/utils/detectFramework.js.map +1 -0
- package/bin/utils/emptyParagraphs.d.ts +3 -0
- package/bin/utils/emptyParagraphs.js +19 -0
- package/bin/utils/emptyParagraphs.js.map +1 -0
- package/bin/utils/errors.d.ts +3 -0
- package/bin/utils/errors.js +16 -0
- package/bin/utils/errors.js.map +1 -0
- package/bin/utils/escape.d.ts +2 -0
- package/bin/utils/escape.js +25 -0
- package/bin/utils/escape.js.map +1 -0
- package/bin/utils/extension.d.ts +3 -0
- package/bin/utils/extension.js +18 -0
- package/bin/utils/extension.js.map +1 -0
- package/bin/utils/file.d.ts +4 -0
- package/bin/utils/file.js +43 -0
- package/bin/utils/file.js.map +1 -0
- package/bin/utils/firstChild.d.ts +2 -0
- package/bin/utils/firstChild.js +12 -0
- package/bin/utils/firstChild.js.map +1 -0
- package/bin/utils/images.d.ts +5 -0
- package/bin/utils/images.js +86 -0
- package/bin/utils/images.js.map +1 -0
- package/bin/utils/img.d.ts +2 -0
- package/bin/utils/img.js +15 -0
- package/bin/utils/img.js.map +1 -0
- package/bin/utils/log.d.ts +18 -0
- package/bin/utils/log.js +68 -0
- package/bin/utils/log.js.map +1 -0
- package/bin/utils/nestedRoots.d.ts +7 -0
- package/bin/utils/nestedRoots.js +19 -0
- package/bin/utils/nestedRoots.js.map +1 -0
- package/bin/utils/network.d.ts +5 -0
- package/bin/utils/network.js +82 -0
- package/bin/utils/network.js.map +1 -0
- package/bin/utils/path.d.ts +1 -0
- package/bin/utils/path.js +22 -0
- package/bin/utils/path.js.map +1 -0
- package/bin/utils/position.d.ts +3 -0
- package/bin/utils/position.js +12 -0
- package/bin/utils/position.js.map +1 -0
- package/bin/utils/reservedNames.d.ts +4 -0
- package/bin/utils/reservedNames.js +27 -0
- package/bin/utils/reservedNames.js.map +1 -0
- package/bin/utils/strings.d.ts +2 -0
- package/bin/utils/strings.js +7 -0
- package/bin/utils/strings.js.map +1 -0
- package/bin/utils/text.d.ts +2 -0
- package/bin/utils/text.js +11 -0
- package/bin/utils/text.js.map +1 -0
- package/bin/utils/title.d.ts +10 -0
- package/bin/utils/title.js +58 -0
- package/bin/utils/title.js.map +1 -0
- package/bin/utils/url.d.ts +3 -0
- package/bin/utils/url.js +10 -0
- package/bin/utils/url.js.map +1 -0
- package/package.json +18 -9
- package/src/assert.ts +15 -0
- package/src/cli.ts +53 -90
- package/src/components/Accordion.ts +84 -0
- package/src/components/AccordionGroup.ts +69 -0
- package/src/components/Callout.ts +159 -0
- package/src/components/Card.ts +168 -0
- package/src/components/CardGroup.ts +69 -0
- package/src/components/CodeGroup.ts +209 -0
- package/src/components/Frame.ts +86 -0
- package/src/components/Tabs.ts +154 -0
- package/src/components/link.ts +17 -0
- package/src/constants.ts +37 -19
- package/src/customComponents/create.ts +106 -0
- package/src/customComponents/plugin.ts +31 -0
- package/src/customComponents/selective.ts +37 -0
- package/src/nav/iterate.ts +18 -0
- package/src/nav/listItems.ts +82 -0
- package/src/nav/retrieve.ts +88 -0
- package/src/nav/root.ts +47 -0
- package/src/openapi/generateOpenApiPages.ts +19 -4
- package/src/root/retrieve.ts +52 -0
- package/src/scrapingPipeline/group.ts +62 -0
- package/src/scrapingPipeline/icon.ts +26 -0
- package/src/scrapingPipeline/images.ts +67 -0
- package/src/scrapingPipeline/logo.ts +127 -0
- package/src/scrapingPipeline/page.ts +130 -0
- package/src/scrapingPipeline/root.ts +10 -0
- package/src/scrapingPipeline/site.ts +161 -0
- package/src/scrapingPipeline/tabs.ts +87 -0
- package/src/tabs/retrieveReadme.ts +99 -0
- package/src/types/components.ts +3 -0
- package/src/types/framework.ts +10 -0
- package/src/types/hast.ts +12 -0
- package/src/types/result.ts +1 -0
- package/src/types/scrapeFunc.ts +9 -0
- package/src/utils/append.ts +9 -0
- package/src/utils/children.ts +51 -0
- package/src/utils/className.ts +14 -0
- package/src/utils/detectFramework.ts +72 -0
- package/src/utils/emptyParagraphs.ts +21 -0
- package/src/utils/errors.ts +24 -0
- package/src/utils/escape.ts +30 -0
- package/src/utils/extension.ts +19 -0
- package/src/utils/file.ts +58 -0
- package/src/utils/firstChild.ts +13 -0
- package/src/utils/images.ts +101 -0
- package/src/utils/img.ts +17 -0
- package/src/utils/log.ts +82 -0
- package/src/utils/nestedRoots.ts +20 -0
- package/src/utils/network.ts +95 -0
- package/src/utils/path.ts +27 -0
- package/src/utils/position.ts +14 -0
- package/src/utils/reservedNames.ts +31 -0
- package/src/utils/strings.ts +7 -0
- package/src/utils/text.ts +11 -0
- package/src/utils/title.ts +68 -0
- package/src/utils/url.ts +8 -0
- package/bin/browser.d.ts +0 -2
- package/bin/browser.js +0 -24
- package/bin/browser.js.map +0 -1
- package/bin/checks.d.ts +0 -8
- package/bin/checks.js +0 -24
- package/bin/checks.js.map +0 -1
- package/bin/downloadImage.d.ts +0 -5
- package/bin/downloadImage.js +0 -88
- package/bin/downloadImage.js.map +0 -1
- package/bin/scraping/combineNavWithEmptyGroupTitles.d.ts +0 -2
- package/bin/scraping/combineNavWithEmptyGroupTitles.js +0 -20
- package/bin/scraping/combineNavWithEmptyGroupTitles.js.map +0 -1
- package/bin/scraping/detectFramework.d.ts +0 -9
- package/bin/scraping/detectFramework.js +0 -36
- package/bin/scraping/detectFramework.js.map +0 -1
- package/bin/scraping/downloadAllImages.d.ts +0 -4
- package/bin/scraping/downloadAllImages.js +0 -36
- package/bin/scraping/downloadAllImages.js.map +0 -1
- package/bin/scraping/downloadLogoImage.d.ts +0 -1
- package/bin/scraping/downloadLogoImage.js +0 -12
- package/bin/scraping/downloadLogoImage.js.map +0 -1
- package/bin/scraping/replaceImagePaths.d.ts +0 -1
- package/bin/scraping/replaceImagePaths.js +0 -14
- package/bin/scraping/replaceImagePaths.js.map +0 -1
- package/bin/scraping/scrapeFileGettingFileNameFromUrl.d.ts +0 -6
- package/bin/scraping/scrapeFileGettingFileNameFromUrl.js +0 -46
- package/bin/scraping/scrapeFileGettingFileNameFromUrl.js.map +0 -1
- package/bin/scraping/scrapeGettingFileNameFromUrl.d.ts +0 -6
- package/bin/scraping/scrapeGettingFileNameFromUrl.js +0 -13
- package/bin/scraping/scrapeGettingFileNameFromUrl.js.map +0 -1
- package/bin/scraping/scrapePage.d.ts +0 -8
- package/bin/scraping/scrapePage.js +0 -10
- package/bin/scraping/scrapePage.js.map +0 -1
- package/bin/scraping/scrapePageCommands.d.ts +0 -7
- package/bin/scraping/scrapePageCommands.js +0 -50
- package/bin/scraping/scrapePageCommands.js.map +0 -1
- package/bin/scraping/scrapeSection.d.ts +0 -3
- package/bin/scraping/scrapeSection.js +0 -12
- package/bin/scraping/scrapeSection.js.map +0 -1
- package/bin/scraping/scrapeSectionCommands.d.ts +0 -6
- package/bin/scraping/scrapeSectionCommands.js +0 -63
- package/bin/scraping/scrapeSectionCommands.js.map +0 -1
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomPage.d.ts +0 -5
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomPage.js +0 -29
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomPage.js.map +0 -1
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomSection.d.ts +0 -2
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomSection.js +0 -31
- package/bin/scraping/site-scrapers/Intercom/scrapeIntercomSection.js.map +0 -1
- package/bin/scraping/site-scrapers/alternateGroupTitle.d.ts +0 -3
- package/bin/scraping/site-scrapers/alternateGroupTitle.js +0 -9
- package/bin/scraping/site-scrapers/alternateGroupTitle.js.map +0 -1
- package/bin/scraping/site-scrapers/links-per-group/getDocusaurusLinksPerGroup.d.ts +0 -5
- package/bin/scraping/site-scrapers/links-per-group/getDocusaurusLinksPerGroup.js +0 -33
- package/bin/scraping/site-scrapers/links-per-group/getDocusaurusLinksPerGroup.js.map +0 -1
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursively.d.ts +0 -3
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursively.js +0 -35
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursively.js.map +0 -1
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursivelyGitBook.d.ts +0 -3
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursivelyGitBook.js +0 -33
- package/bin/scraping/site-scrapers/links-per-group/getLinksRecursivelyGitBook.js.map +0 -1
- package/bin/scraping/site-scrapers/openNestedDocusaurusMenus.d.ts +0 -2
- package/bin/scraping/site-scrapers/openNestedDocusaurusMenus.js +0 -30
- package/bin/scraping/site-scrapers/openNestedDocusaurusMenus.js.map +0 -1
- package/bin/scraping/site-scrapers/openNestedGitbookMenus.d.ts +0 -2
- package/bin/scraping/site-scrapers/openNestedGitbookMenus.js +0 -21
- package/bin/scraping/site-scrapers/openNestedGitbookMenus.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeDocusaurusPage.d.ts +0 -5
- package/bin/scraping/site-scrapers/scrapeDocusaurusPage.js +0 -53
- package/bin/scraping/site-scrapers/scrapeDocusaurusPage.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeDocusaurusSection.d.ts +0 -2
- package/bin/scraping/site-scrapers/scrapeDocusaurusSection.js +0 -32
- package/bin/scraping/site-scrapers/scrapeDocusaurusSection.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeGitBookPage.d.ts +0 -5
- package/bin/scraping/site-scrapers/scrapeGitBookPage.js +0 -56
- package/bin/scraping/site-scrapers/scrapeGitBookPage.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeGitBookSection.d.ts +0 -2
- package/bin/scraping/site-scrapers/scrapeGitBookSection.js +0 -42
- package/bin/scraping/site-scrapers/scrapeGitBookSection.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeReadMePage.d.ts +0 -5
- package/bin/scraping/site-scrapers/scrapeReadMePage.js +0 -38
- package/bin/scraping/site-scrapers/scrapeReadMePage.js.map +0 -1
- package/bin/scraping/site-scrapers/scrapeReadMeSection.d.ts +0 -2
- package/bin/scraping/site-scrapers/scrapeReadMeSection.js +0 -39
- package/bin/scraping/site-scrapers/scrapeReadMeSection.js.map +0 -1
- package/bin/util.d.ts +0 -29
- package/bin/util.js +0 -97
- package/bin/util.js.map +0 -1
- package/src/browser.ts +0 -24
- package/src/checks.ts +0 -32
- package/src/downloadImage.ts +0 -102
- package/src/scraping/combineNavWithEmptyGroupTitles.ts +0 -21
- package/src/scraping/detectFramework.ts +0 -55
- package/src/scraping/downloadAllImages.ts +0 -61
- package/src/scraping/downloadLogoImage.ts +0 -24
- package/src/scraping/replaceImagePaths.ts +0 -17
- package/src/scraping/scrapeFileGettingFileNameFromUrl.ts +0 -84
- package/src/scraping/scrapeGettingFileNameFromUrl.ts +0 -56
- package/src/scraping/scrapePage.ts +0 -40
- package/src/scraping/scrapePageCommands.ts +0 -68
- package/src/scraping/scrapeSection.ts +0 -30
- package/src/scraping/scrapeSectionCommands.ts +0 -98
- package/src/scraping/site-scrapers/Intercom/scrapeIntercomPage.ts +0 -52
- package/src/scraping/site-scrapers/Intercom/scrapeIntercomSection.ts +0 -54
- package/src/scraping/site-scrapers/alternateGroupTitle.ts +0 -11
- package/src/scraping/site-scrapers/links-per-group/getDocusaurusLinksPerGroup.ts +0 -45
- package/src/scraping/site-scrapers/links-per-group/getLinksRecursively.ts +0 -47
- package/src/scraping/site-scrapers/links-per-group/getLinksRecursivelyGitBook.ts +0 -44
- package/src/scraping/site-scrapers/openNestedDocusaurusMenus.ts +0 -42
- package/src/scraping/site-scrapers/openNestedGitbookMenus.ts +0 -27
- package/src/scraping/site-scrapers/scrapeDocusaurusPage.ts +0 -85
- package/src/scraping/site-scrapers/scrapeDocusaurusSection.ts +0 -63
- package/src/scraping/site-scrapers/scrapeGitBookPage.ts +0 -82
- package/src/scraping/site-scrapers/scrapeGitBookSection.ts +0 -69
- package/src/scraping/site-scrapers/scrapeReadMePage.ts +0 -56
- package/src/scraping/site-scrapers/scrapeReadMeSection.ts +0 -66
- package/src/util.ts +0 -122
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Root as HastRoot, Element } from 'hast';
|
|
2
|
+
import type { BlockContent, DefinitionContent } from 'mdast';
|
|
3
|
+
import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
|
|
4
|
+
import { visit } from 'unist-util-visit';
|
|
5
|
+
|
|
6
|
+
import { ESCAPED_COMPONENTS } from '../constants.js';
|
|
7
|
+
import type { EscapedComponent } from '../types/components.js';
|
|
8
|
+
|
|
9
|
+
export function rehypeToRemarkCustomComponents() {
|
|
10
|
+
return function (tree: HastRoot) {
|
|
11
|
+
visit(tree, 'element', function (node: Element, index, parent) {
|
|
12
|
+
if (ESCAPED_COMPONENTS.includes(node.tagName as unknown as EscapedComponent)) {
|
|
13
|
+
const newNode: MdxJsxFlowElement = {
|
|
14
|
+
type: 'mdxJsxFlowElement',
|
|
15
|
+
name: node.tagName,
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
17
|
+
attributes: Object.entries(node.properties || {}).map(([key, value]) => ({
|
|
18
|
+
type: 'mdxJsxAttribute',
|
|
19
|
+
name: key,
|
|
20
|
+
value: value as string,
|
|
21
|
+
})),
|
|
22
|
+
children: node.children as Array<BlockContent | DefinitionContent>,
|
|
23
|
+
};
|
|
24
|
+
if (parent && typeof index === 'number') {
|
|
25
|
+
parent.children[index] = newNode as unknown as Element;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return tree;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Root as HastRoot, Element } from 'hast';
|
|
2
|
+
import { toMdast, defaultHandlers } from 'hast-util-to-mdast';
|
|
3
|
+
import type { State, Handle } from 'hast-util-to-mdast';
|
|
4
|
+
import type { Root as MdastRoot } from 'mdast';
|
|
5
|
+
import type { BlockContent, DefinitionContent } from 'mdast';
|
|
6
|
+
import type { MdxJsxFlowElement } from 'mdast-util-mdx-jsx';
|
|
7
|
+
|
|
8
|
+
import { ESCAPED_COMPONENTS } from '../constants.js';
|
|
9
|
+
|
|
10
|
+
export function mdxJsxFlowElementHandler(_: State, node: Element): MdxJsxFlowElement {
|
|
11
|
+
return {
|
|
12
|
+
type: 'mdxJsxFlowElement',
|
|
13
|
+
name: node.tagName,
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
15
|
+
attributes: Object.entries(node.properties ?? {}).map(([key, value]) => ({
|
|
16
|
+
type: 'mdxJsxAttribute',
|
|
17
|
+
name: key,
|
|
18
|
+
value: value as string,
|
|
19
|
+
})),
|
|
20
|
+
children: node.children as Array<BlockContent | DefinitionContent>,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function selectiveRehypeRemark() {
|
|
25
|
+
const handlers: Record<string, Handle> = { ...defaultHandlers };
|
|
26
|
+
ESCAPED_COMPONENTS.forEach((tagName) => {
|
|
27
|
+
handlers[tagName] = mdxJsxFlowElementHandler;
|
|
28
|
+
});
|
|
29
|
+
handlers.mdxJsxFlowElement = mdxJsxFlowElementHandler;
|
|
30
|
+
|
|
31
|
+
return function (tree: HastRoot) {
|
|
32
|
+
const newTree = toMdast(tree, {
|
|
33
|
+
handlers,
|
|
34
|
+
}) as MdastRoot;
|
|
35
|
+
return newTree;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NavigationEntry } from '@mintlify/models';
|
|
2
|
+
|
|
3
|
+
export function iterateOverNavItems(navItems: Array<NavigationEntry>, origin: string): Array<URL> {
|
|
4
|
+
return navItems.flatMap((navItem) => recurseOverGroup(navItem, origin));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function recurseOverGroup(group: NavigationEntry, origin: string): Array<URL> {
|
|
8
|
+
if (typeof group === 'string') {
|
|
9
|
+
return [new URL(group, origin)];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return group.pages.flatMap((pageOrGroup: string | NavigationEntry) => {
|
|
13
|
+
if (typeof pageOrGroup === 'string') {
|
|
14
|
+
return new URL(pageOrGroup, origin);
|
|
15
|
+
}
|
|
16
|
+
return recurseOverGroup(pageOrGroup, origin);
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { NavigationEntry } from '@mintlify/models';
|
|
2
|
+
import type { Element } from 'hast';
|
|
3
|
+
import { EXIT, visit } from 'unist-util-visit';
|
|
4
|
+
|
|
5
|
+
import { dedupedAppend } from '../utils/append.js';
|
|
6
|
+
import { framework } from '../utils/detectFramework.js';
|
|
7
|
+
import { findFirstChild } from '../utils/firstChild.js';
|
|
8
|
+
import { removeTrailingSlash } from '../utils/strings.js';
|
|
9
|
+
import { getText } from '../utils/text.js';
|
|
10
|
+
import { retrieveNavItems } from './retrieve.js';
|
|
11
|
+
|
|
12
|
+
export type ListItemOptions = {
|
|
13
|
+
sectionTagName: string;
|
|
14
|
+
childListTagName: string;
|
|
15
|
+
title?: string;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export function processListItem(
|
|
19
|
+
node: Element,
|
|
20
|
+
opts: ListItemOptions = {
|
|
21
|
+
sectionTagName: 'div',
|
|
22
|
+
childListTagName: 'ul',
|
|
23
|
+
title: undefined,
|
|
24
|
+
}
|
|
25
|
+
): NavigationEntry | undefined {
|
|
26
|
+
const link = findFirstChild(node, 'a');
|
|
27
|
+
if (!link) return undefined;
|
|
28
|
+
|
|
29
|
+
let linkHref: string | undefined = undefined;
|
|
30
|
+
linkHref = link.properties.href as string | undefined;
|
|
31
|
+
|
|
32
|
+
if (linkHref === undefined || linkHref === '#') {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let isApiReferenceLink = false;
|
|
37
|
+
visit(link, 'element', function (subNode) {
|
|
38
|
+
if (
|
|
39
|
+
subNode.tagName === 'span' &&
|
|
40
|
+
Array.isArray(subNode.properties.className) &&
|
|
41
|
+
subNode.properties.className.includes('rm-APIMethod')
|
|
42
|
+
) {
|
|
43
|
+
isApiReferenceLink = true;
|
|
44
|
+
return EXIT;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
48
|
+
if (isApiReferenceLink) return undefined;
|
|
49
|
+
|
|
50
|
+
if (linkHref.startsWith('/')) linkHref = linkHref.substring(1);
|
|
51
|
+
|
|
52
|
+
const sectionHeader = findFirstChild(node, opts.sectionTagName);
|
|
53
|
+
const childList = findFirstChild(node, opts.childListTagName);
|
|
54
|
+
let title = opts.title;
|
|
55
|
+
if (!title) {
|
|
56
|
+
title = getText(link) || getText(sectionHeader) || '';
|
|
57
|
+
if (framework.vendor === 'readme') {
|
|
58
|
+
title = getText(sectionHeader) || getText(link) || '';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!childList) {
|
|
63
|
+
return linkHref;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let childEntries = retrieveNavItems(childList);
|
|
67
|
+
const newLink = childEntries.find(
|
|
68
|
+
(child) => typeof child === 'string' && child.startsWith(linkHref)
|
|
69
|
+
)
|
|
70
|
+
? removeTrailingSlash(linkHref) + '/mintie_overview'
|
|
71
|
+
: linkHref;
|
|
72
|
+
|
|
73
|
+
if (childEntries.includes(linkHref)) {
|
|
74
|
+
childEntries.forEach((child, index) => {
|
|
75
|
+
if (child === linkHref) childEntries[index] = newLink;
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
childEntries = dedupedAppend(newLink, childEntries, true);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return { group: title, pages: dedupedAppend(newLink, childEntries, true) };
|
|
82
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { NavigationEntry } from '@mintlify/models';
|
|
2
|
+
import type { Element } from 'hast';
|
|
3
|
+
import { visit, CONTINUE, SKIP } from 'unist-util-visit';
|
|
4
|
+
|
|
5
|
+
import { framework } from '../utils/detectFramework.js';
|
|
6
|
+
import { findTitle } from '../utils/title.js';
|
|
7
|
+
import { processListItem } from './listItems.js';
|
|
8
|
+
|
|
9
|
+
export function retrieveNavItems(rootNode: Element): Array<NavigationEntry> {
|
|
10
|
+
const result: Array<NavigationEntry> = [];
|
|
11
|
+
|
|
12
|
+
let rootSectionTagName = 'li';
|
|
13
|
+
switch (framework.vendor) {
|
|
14
|
+
case 'docusaurus':
|
|
15
|
+
rootSectionTagName = 'li';
|
|
16
|
+
break;
|
|
17
|
+
case 'gitbook':
|
|
18
|
+
rootSectionTagName = 'li';
|
|
19
|
+
break;
|
|
20
|
+
case 'readme':
|
|
21
|
+
rootSectionTagName = 'section';
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
let innerSectionTagName = 'div';
|
|
26
|
+
switch (framework.vendor) {
|
|
27
|
+
case 'docusaurus':
|
|
28
|
+
innerSectionTagName = 'li';
|
|
29
|
+
break;
|
|
30
|
+
case 'gitbook':
|
|
31
|
+
innerSectionTagName = 'div';
|
|
32
|
+
break;
|
|
33
|
+
case 'readme':
|
|
34
|
+
innerSectionTagName = 'h2';
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
visit(rootNode, 'element', function (node, index, parent) {
|
|
39
|
+
if (node.tagName === rootSectionTagName) node.tagName = 'li';
|
|
40
|
+
if (node.tagName !== 'li') return CONTINUE;
|
|
41
|
+
|
|
42
|
+
let title: string | undefined = undefined;
|
|
43
|
+
if (
|
|
44
|
+
node.children[0] &&
|
|
45
|
+
node.children[1] &&
|
|
46
|
+
node.children[1].type === 'element' &&
|
|
47
|
+
node.children[1].tagName === 'ul' &&
|
|
48
|
+
node.children[0].type === 'element' &&
|
|
49
|
+
node.children[0].tagName === 'div' &&
|
|
50
|
+
node.children[0].children.filter((child) => child.type === 'text').length ===
|
|
51
|
+
node.children[0].children.length
|
|
52
|
+
)
|
|
53
|
+
title = findTitle(node.children[0], { delete: false });
|
|
54
|
+
|
|
55
|
+
if (
|
|
56
|
+
framework.vendor === 'readme' &&
|
|
57
|
+
node.children.length === 2 &&
|
|
58
|
+
node.children[1] &&
|
|
59
|
+
node.children[1].type === 'element' &&
|
|
60
|
+
node.children[1].tagName === 'ul' &&
|
|
61
|
+
typeof index === 'number' &&
|
|
62
|
+
parent
|
|
63
|
+
) {
|
|
64
|
+
node.children = [
|
|
65
|
+
{
|
|
66
|
+
type: 'element',
|
|
67
|
+
tagName: 'div',
|
|
68
|
+
properties: {},
|
|
69
|
+
children: node.children,
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const entry = processListItem(node, {
|
|
75
|
+
sectionTagName: innerSectionTagName,
|
|
76
|
+
childListTagName: 'ul',
|
|
77
|
+
title,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (entry !== undefined) {
|
|
81
|
+
result.push(entry);
|
|
82
|
+
return SKIP;
|
|
83
|
+
}
|
|
84
|
+
return CONTINUE;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
}
|
package/src/nav/root.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { Root as HastRoot, Element } from 'hast';
|
|
2
|
+
import { visit, EXIT } from 'unist-util-visit';
|
|
3
|
+
|
|
4
|
+
import { framework } from '../utils/detectFramework.js';
|
|
5
|
+
|
|
6
|
+
export function retrieveRootNavElement(rootNode: HastRoot): Element | undefined {
|
|
7
|
+
let rootTagName = 'aside';
|
|
8
|
+
switch (framework.vendor) {
|
|
9
|
+
case 'docusaurus':
|
|
10
|
+
rootTagName = 'nav';
|
|
11
|
+
break;
|
|
12
|
+
case 'gitbook':
|
|
13
|
+
rootTagName = 'aside';
|
|
14
|
+
break;
|
|
15
|
+
case 'readme':
|
|
16
|
+
rootTagName = 'nav';
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let rootSelector = 'page-no-toc:hidden';
|
|
21
|
+
switch (framework.vendor) {
|
|
22
|
+
case 'docusaurus':
|
|
23
|
+
rootSelector = 'menu';
|
|
24
|
+
break;
|
|
25
|
+
case 'gitbook':
|
|
26
|
+
rootSelector = 'page-no-toc:hidden';
|
|
27
|
+
break;
|
|
28
|
+
case 'readme':
|
|
29
|
+
rootSelector = 'rm-Sidebar';
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let element: Element | undefined = undefined;
|
|
34
|
+
visit(rootNode, 'element', function (node) {
|
|
35
|
+
if (
|
|
36
|
+
node.tagName === rootTagName &&
|
|
37
|
+
node.properties.className &&
|
|
38
|
+
Array.isArray(node.properties.className) &&
|
|
39
|
+
node.properties.className.includes(rootSelector)
|
|
40
|
+
) {
|
|
41
|
+
element = node;
|
|
42
|
+
return EXIT;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return element;
|
|
47
|
+
}
|
|
@@ -39,13 +39,28 @@ export const generateOpenApiPages = async (
|
|
|
39
39
|
|
|
40
40
|
const { errors, schema, valid, version } = await validate(pathOrDocument);
|
|
41
41
|
|
|
42
|
+
if (!version || version === '2.0') throw new Error('OpenAPI versions prior to 3.0 not supported');
|
|
43
|
+
|
|
42
44
|
if (!valid) {
|
|
43
|
-
const errorMessages = errors
|
|
45
|
+
const errorMessages = errors
|
|
46
|
+
? errors
|
|
47
|
+
.map((error) => {
|
|
48
|
+
const path =
|
|
49
|
+
'path' in error
|
|
50
|
+
? String(error.path)
|
|
51
|
+
.replace('/paths/', '')
|
|
52
|
+
.replaceAll('~1', '/')
|
|
53
|
+
.replaceAll('~2', '~')
|
|
54
|
+
: 'Unknown path';
|
|
55
|
+
return `${path}: ${error.message}`;
|
|
56
|
+
})
|
|
57
|
+
.join('\n')
|
|
58
|
+
: 'An unknown error occurred when trying to validate your OpenAPI spec. Please pass it through an OpenAPI validator and try again.';
|
|
59
|
+
console.error(errorMessages);
|
|
44
60
|
throw new Error(`Failed to validate OpenAPI schema:${errorMessages}`);
|
|
45
61
|
}
|
|
46
62
|
|
|
47
63
|
if (!schema) throw new Error('Schema failed to validate. Please try again.');
|
|
48
|
-
if (!version || version === '2.0') throw new Error('OpenAPI versions prior to 3.0 not supported');
|
|
49
64
|
|
|
50
65
|
if (schema.paths === undefined || Object.keys(schema.paths).length === 0) {
|
|
51
66
|
throw new Error('No paths defined.');
|
|
@@ -152,8 +167,8 @@ const createOpenApiFrontmatter = async (
|
|
|
152
167
|
path: string
|
|
153
168
|
) => {
|
|
154
169
|
const data = `---
|
|
155
|
-
openapi: ${method} ${path}
|
|
156
|
-
---`;
|
|
170
|
+
openapi: ${method} ${path}
|
|
171
|
+
---`;
|
|
157
172
|
|
|
158
173
|
await outputFile(filename, data);
|
|
159
174
|
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Root as HastRoot, Element } from 'hast';
|
|
2
|
+
import { visit, EXIT } from 'unist-util-visit';
|
|
3
|
+
|
|
4
|
+
import { framework } from '../utils/detectFramework.js';
|
|
5
|
+
|
|
6
|
+
export function retrieveRootContent(rootNode: HastRoot): Element | undefined {
|
|
7
|
+
let rootTagName = 'main';
|
|
8
|
+
switch (framework.vendor) {
|
|
9
|
+
case 'docusaurus':
|
|
10
|
+
rootTagName = 'article';
|
|
11
|
+
break;
|
|
12
|
+
case 'gitbook':
|
|
13
|
+
rootTagName = 'main';
|
|
14
|
+
break;
|
|
15
|
+
case 'readme':
|
|
16
|
+
rootTagName = 'section';
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let rootSelector = 'break-anywhere';
|
|
21
|
+
switch (framework.vendor) {
|
|
22
|
+
case 'docusaurus':
|
|
23
|
+
rootSelector = '';
|
|
24
|
+
break;
|
|
25
|
+
case 'gitbook':
|
|
26
|
+
rootSelector = '';
|
|
27
|
+
break;
|
|
28
|
+
case 'readme':
|
|
29
|
+
rootSelector = 'content-body';
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let element: Element | undefined = undefined;
|
|
34
|
+
visit(rootNode, 'element', function (node) {
|
|
35
|
+
if (node.tagName === rootTagName) {
|
|
36
|
+
if (rootSelector) {
|
|
37
|
+
if (
|
|
38
|
+
node.properties.className &&
|
|
39
|
+
Array.isArray(node.properties.className) &&
|
|
40
|
+
node.properties.className.includes(rootSelector)
|
|
41
|
+
) {
|
|
42
|
+
element = node;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
element = node;
|
|
46
|
+
return EXIT;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return element;
|
|
52
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Browser } from 'puppeteer';
|
|
2
|
+
|
|
3
|
+
import type { Result } from '../types/result.js';
|
|
4
|
+
import { getErrorMessage } from '../utils/errors.js';
|
|
5
|
+
import { log } from '../utils/log.js';
|
|
6
|
+
import { fetchPageHtml, startPuppeteer } from '../utils/network.js';
|
|
7
|
+
import { scrapePage } from './page.js';
|
|
8
|
+
|
|
9
|
+
export async function scrapePageGroup(
|
|
10
|
+
navGroup: Array<URL>,
|
|
11
|
+
needsBrowser: boolean,
|
|
12
|
+
opts: {
|
|
13
|
+
externalLinks: boolean;
|
|
14
|
+
rootPaths?: Array<string>;
|
|
15
|
+
} = { externalLinks: false }
|
|
16
|
+
): Promise<Array<Result<[string, string]>>> {
|
|
17
|
+
const browser: Browser | undefined = needsBrowser ? await startPuppeteer() : undefined;
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const res = await Promise.all(
|
|
21
|
+
navGroup.map(async (url, index) => {
|
|
22
|
+
try {
|
|
23
|
+
if (opts.externalLinks) {
|
|
24
|
+
const res = scrapePage(`external-link-${index}`, url, { externalLink: true });
|
|
25
|
+
return res;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let isOverviewPage = false;
|
|
29
|
+
if (url.toString().endsWith('/mintie_overview')) {
|
|
30
|
+
isOverviewPage = true;
|
|
31
|
+
url = new URL(url.toString().replace('/mintie_overview', ''));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const html = await fetchPageHtml(url, browser);
|
|
35
|
+
const res = scrapePage(html, url, {
|
|
36
|
+
externalLink: false,
|
|
37
|
+
isOverviewPage,
|
|
38
|
+
rootPath: opts.rootPaths ? opts.rootPaths[index] : undefined,
|
|
39
|
+
});
|
|
40
|
+
return res;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
const errorMessage = getErrorMessage(error);
|
|
43
|
+
throw new Error(`We encountered an error when scraping ${url}${errorMessage}`);
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
return res;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
const errorMessage = getErrorMessage(error);
|
|
50
|
+
log(
|
|
51
|
+
`We encountered an error when scraping the page group from ${
|
|
52
|
+
navGroup[0]?.origin ?? 'the URL provided'
|
|
53
|
+
}${errorMessage}`
|
|
54
|
+
);
|
|
55
|
+
console.error(error);
|
|
56
|
+
throw error;
|
|
57
|
+
} finally {
|
|
58
|
+
if (browser) {
|
|
59
|
+
await browser.close();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Root as HastRoot } from 'hast';
|
|
2
|
+
import { EXIT, visit } from 'unist-util-visit';
|
|
3
|
+
|
|
4
|
+
import { downloadImage } from '../utils/images.js';
|
|
5
|
+
|
|
6
|
+
export async function downloadFavicon(hast: HastRoot): Promise<string | undefined> {
|
|
7
|
+
let src: string = '';
|
|
8
|
+
visit(hast, 'element', function (node) {
|
|
9
|
+
if (
|
|
10
|
+
node.tagName === 'link' &&
|
|
11
|
+
typeof node.properties.rel === 'string' &&
|
|
12
|
+
(node.properties.rel === 'icon' || node.properties.rel === 'shortcut icon')
|
|
13
|
+
) {
|
|
14
|
+
src = node.properties.href as string;
|
|
15
|
+
return EXIT;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
if (!src) return undefined;
|
|
20
|
+
|
|
21
|
+
const res = await downloadImage(src, process.cwd());
|
|
22
|
+
if (!res.success) return undefined;
|
|
23
|
+
if (!res.data) return undefined;
|
|
24
|
+
|
|
25
|
+
return res.data[1];
|
|
26
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Root as MdastRoot } from 'mdast';
|
|
2
|
+
import type { MdxJsxAttribute } from 'mdast-util-mdx-jsx';
|
|
3
|
+
import { join, dirname } from 'node:path';
|
|
4
|
+
import { CONTINUE, visit } from 'unist-util-visit';
|
|
5
|
+
|
|
6
|
+
import type { Result } from '../types/result.js';
|
|
7
|
+
import { downloadImage } from '../utils/images.js';
|
|
8
|
+
import { createFilename } from '../utils/path.js';
|
|
9
|
+
|
|
10
|
+
export async function downloadImagesFromFile(
|
|
11
|
+
root: MdastRoot,
|
|
12
|
+
url: string | URL
|
|
13
|
+
): Promise<Array<Result<[string, string]>>> {
|
|
14
|
+
url = new URL(url);
|
|
15
|
+
const imageUrls: Array<string> = [];
|
|
16
|
+
|
|
17
|
+
visit(root, function (node) {
|
|
18
|
+
let imageUrl: string | undefined = undefined;
|
|
19
|
+
if (node.type === 'image') {
|
|
20
|
+
imageUrl = node.url;
|
|
21
|
+
} else if (node.type === 'mdxJsxFlowElement') {
|
|
22
|
+
imageUrl = node.attributes.find(
|
|
23
|
+
(attr) => attr.type === 'mdxJsxAttribute' && (attr.name === 'src' || attr.name === 'img')
|
|
24
|
+
)?.value as string | undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!imageUrl) return CONTINUE;
|
|
28
|
+
|
|
29
|
+
if (imageUrl.startsWith('/')) {
|
|
30
|
+
imageUrl = new URL(imageUrl, url.origin).toString();
|
|
31
|
+
}
|
|
32
|
+
imageUrls.push(imageUrl);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const rootPath = join(process.cwd(), '/images');
|
|
36
|
+
const filename = createFilename(rootPath, url, '');
|
|
37
|
+
const localRootPath = filename ? dirname(filename) : rootPath;
|
|
38
|
+
|
|
39
|
+
const imageResults = await Promise.all(
|
|
40
|
+
imageUrls.map(async (imageUrl) => {
|
|
41
|
+
const result = await downloadImage(imageUrl, localRootPath);
|
|
42
|
+
return result;
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const imagePathsMap = new Map<string, string>(
|
|
47
|
+
imageResults.filter((result) => result.success).map((result) => result.data as [string, string])
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
visit(root, function (node, index, parent) {
|
|
51
|
+
if (node.type === 'image') {
|
|
52
|
+
node.url = imagePathsMap.get(node.url) ?? node.url;
|
|
53
|
+
if (parent && typeof index === 'number') parent.children[index] = node;
|
|
54
|
+
} else if (node.type === 'mdxJsxFlowElement') {
|
|
55
|
+
const urlAttr = (node.attributes as Array<MdxJsxAttribute>).find(
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
57
|
+
(attr) => attr.type === 'mdxJsxAttribute' && (attr.name === 'src' || attr.name === 'img')
|
|
58
|
+
);
|
|
59
|
+
if (!urlAttr) return CONTINUE;
|
|
60
|
+
|
|
61
|
+
urlAttr.value = imagePathsMap.get(urlAttr.value as string) ?? urlAttr.value;
|
|
62
|
+
if (parent && typeof index === 'number') parent.children[index] = node;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return imageResults;
|
|
67
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { Root as HastRoot, Element } from 'hast';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { Browser } from 'puppeteer';
|
|
4
|
+
import { EXIT, visit } from 'unist-util-visit';
|
|
5
|
+
|
|
6
|
+
import { framework } from '../utils/detectFramework.js';
|
|
7
|
+
import { downloadImage } from '../utils/images.js';
|
|
8
|
+
import { fetchPageHtml } from '../utils/network.js';
|
|
9
|
+
import { htmlToHast } from './root.js';
|
|
10
|
+
|
|
11
|
+
function findReadmeLogoNodes(root: HastRoot): Array<Element> | undefined {
|
|
12
|
+
const elements: Array<Element> = [];
|
|
13
|
+
visit(root, 'element', function (node) {
|
|
14
|
+
if (
|
|
15
|
+
node.tagName === 'img' &&
|
|
16
|
+
Array.isArray(node.properties.className) &&
|
|
17
|
+
node.properties.className.includes('rm-Logo-img')
|
|
18
|
+
)
|
|
19
|
+
elements.push(node);
|
|
20
|
+
});
|
|
21
|
+
return elements.length ? elements : undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function findGitBookLogoNodes(root: HastRoot): Array<Element> | undefined {
|
|
25
|
+
const elements: Array<Element> = [];
|
|
26
|
+
visit(root, 'element', function (node) {
|
|
27
|
+
if (node.tagName === 'img' && node.properties.alt === 'Logo') {
|
|
28
|
+
elements.push(node);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
return elements.length ? elements : undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function findDocusaurusLogoNodes(root: HastRoot): Array<Element> | undefined {
|
|
35
|
+
const elements: Array<Element> = [];
|
|
36
|
+
visit(root, 'element', function (node) {
|
|
37
|
+
if (
|
|
38
|
+
node.tagName === 'div' &&
|
|
39
|
+
Array.isArray(node.properties.className) &&
|
|
40
|
+
node.properties.className.includes('navbar__brand')
|
|
41
|
+
) {
|
|
42
|
+
visit(node, 'element', function (subNode) {
|
|
43
|
+
if (subNode.tagName === 'img') elements.push(subNode);
|
|
44
|
+
});
|
|
45
|
+
return EXIT;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return elements.length ? elements : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function findLogosFromHtml(
|
|
52
|
+
html: string,
|
|
53
|
+
downloadFn: (root: HastRoot) => Array<Element> | undefined,
|
|
54
|
+
filepaths: Array<string>
|
|
55
|
+
) {
|
|
56
|
+
const hast = htmlToHast(html);
|
|
57
|
+
const imgNodes = downloadFn(hast);
|
|
58
|
+
|
|
59
|
+
if (imgNodes) {
|
|
60
|
+
filepaths.push(
|
|
61
|
+
...(await Promise.all(
|
|
62
|
+
imgNodes.map(async (node) => {
|
|
63
|
+
const res = await downloadImage(
|
|
64
|
+
node.properties.src as string,
|
|
65
|
+
join(process.cwd(), 'images')
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
if (res.success && res.data) {
|
|
69
|
+
return res.data[1];
|
|
70
|
+
} else {
|
|
71
|
+
return '';
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
))
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
filepaths.forEach((filepath, index) => {
|
|
79
|
+
if (!filepath) filepaths.splice(index, 1);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function downloadLogos(
|
|
84
|
+
url: string | URL,
|
|
85
|
+
browser: Browser | undefined
|
|
86
|
+
): Promise<string | { light: string; dark: string } | undefined> {
|
|
87
|
+
url = new URL(url);
|
|
88
|
+
const filepaths: Array<string> = [];
|
|
89
|
+
if (browser) {
|
|
90
|
+
const htmls: Array<string> = [];
|
|
91
|
+
|
|
92
|
+
const page = await browser.newPage();
|
|
93
|
+
await page.goto(url.toString(), {
|
|
94
|
+
waitUntil: 'networkidle2',
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
htmls.push(await page.content());
|
|
98
|
+
await page.click('.rm-ThemeToggle');
|
|
99
|
+
htmls.push(await page.content());
|
|
100
|
+
|
|
101
|
+
await Promise.all(
|
|
102
|
+
htmls.map(async (html) => {
|
|
103
|
+
return await findLogosFromHtml(html, findReadmeLogoNodes, filepaths);
|
|
104
|
+
})
|
|
105
|
+
);
|
|
106
|
+
} else {
|
|
107
|
+
const html = await fetchPageHtml(url);
|
|
108
|
+
await findLogosFromHtml(
|
|
109
|
+
html,
|
|
110
|
+
framework.vendor === 'gitbook' ? findGitBookLogoNodes : findDocusaurusLogoNodes,
|
|
111
|
+
filepaths
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (browser) await browser.close();
|
|
116
|
+
|
|
117
|
+
const uniqueFilepaths = [...new Set(filepaths).values()];
|
|
118
|
+
|
|
119
|
+
return uniqueFilepaths.length === 1
|
|
120
|
+
? uniqueFilepaths[0]
|
|
121
|
+
: uniqueFilepaths.length > 1
|
|
122
|
+
? {
|
|
123
|
+
light: uniqueFilepaths[0] as string,
|
|
124
|
+
dark: uniqueFilepaths[1] as string,
|
|
125
|
+
}
|
|
126
|
+
: undefined;
|
|
127
|
+
}
|