@rocket/js 0.0.0 → 0.1.1
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/LICENSE +21 -0
- package/README.md +233 -2
- package/dist-types/exports/MainMenu.d.ts +2 -0
- package/dist-types/exports/MainMenu.d.ts.map +1 -0
- package/dist-types/exports/PageData.d.ts +2 -0
- package/dist-types/exports/PageData.d.ts.map +1 -0
- package/dist-types/exports/RocketCodeBlock.d.ts +2 -0
- package/dist-types/exports/RocketCodeBlock.d.ts.map +1 -0
- package/dist-types/exports/RocketIcon.d.ts +2 -0
- package/dist-types/exports/RocketIcon.d.ts.map +1 -0
- package/dist-types/exports/RocketJsDemo.d.ts +2 -0
- package/dist-types/exports/RocketJsDemo.d.ts.map +1 -0
- package/dist-types/exports/RocketRequestDemo.d.ts +2 -0
- package/dist-types/exports/RocketRequestDemo.d.ts.map +1 -0
- package/dist-types/exports/SocialPreviewPlayground.d.ts +2 -0
- package/dist-types/exports/SocialPreviewPlayground.d.ts.map +1 -0
- package/dist-types/exports/adapters/netlify.d.ts +2 -0
- package/dist-types/exports/adapters/netlify.d.ts.map +1 -0
- package/dist-types/exports/asyncMessage.d.ts +2 -0
- package/dist-types/exports/asyncMessage.d.ts.map +1 -0
- package/dist-types/exports/component-hydration.d.ts +2 -0
- package/dist-types/exports/component-hydration.d.ts.map +1 -0
- package/dist-types/exports/components/web-awesome.d.ts +3 -0
- package/dist-types/exports/components/web-awesome.d.ts.map +1 -0
- package/dist-types/exports/components.d.ts +2 -0
- package/dist-types/exports/components.d.ts.map +1 -0
- package/dist-types/exports/config.d.ts +2 -0
- package/dist-types/exports/config.d.ts.map +1 -0
- package/dist-types/exports/debounce.d.ts +2 -0
- package/dist-types/exports/debounce.d.ts.map +1 -0
- package/dist-types/exports/define/RocketCodeBlock.d.ts +2 -0
- package/dist-types/exports/define/RocketCodeBlock.d.ts.map +1 -0
- package/dist-types/exports/define/RocketIcon.d.ts +2 -0
- package/dist-types/exports/define/RocketIcon.d.ts.map +1 -0
- package/dist-types/exports/define/RocketJsDemo.d.ts +2 -0
- package/dist-types/exports/define/RocketJsDemo.d.ts.map +1 -0
- package/dist-types/exports/define/RocketRequestDemo.d.ts +2 -0
- package/dist-types/exports/define/RocketRequestDemo.d.ts.map +1 -0
- package/dist-types/exports/define/menus.d.ts +2 -0
- package/dist-types/exports/define/menus.d.ts.map +1 -0
- package/dist-types/exports/extractCode.d.ts +2 -0
- package/dist-types/exports/extractCode.d.ts.map +1 -0
- package/dist-types/exports/globalData.d.ts +2 -0
- package/dist-types/exports/globalData.d.ts.map +1 -0
- package/dist-types/exports/hydration/hydrationLoader.d.ts +2 -0
- package/dist-types/exports/hydration/hydrationLoader.d.ts.map +1 -0
- package/dist-types/exports/icons.d.ts +2 -0
- package/dist-types/exports/icons.d.ts.map +1 -0
- package/dist-types/exports/layout-helper.d.ts +2 -0
- package/dist-types/exports/layout-helper.d.ts.map +1 -0
- package/dist-types/exports/layout.d.ts +2 -0
- package/dist-types/exports/layout.d.ts.map +1 -0
- package/dist-types/exports/layouts/atlasDoc.d.ts +2 -0
- package/dist-types/exports/layouts/atlasDoc.d.ts.map +1 -0
- package/dist-types/exports/layouts/atlasHero.d.ts +2 -0
- package/dist-types/exports/layouts/atlasHero.d.ts.map +1 -0
- package/dist-types/exports/layouts/atlasNotFound.d.ts +2 -0
- package/dist-types/exports/layouts/atlasNotFound.d.ts.map +1 -0
- package/dist-types/exports/loaded-page-module.d.ts +2 -0
- package/dist-types/exports/loaded-page-module.d.ts.map +1 -0
- package/dist-types/exports/markdownHook.d.ts +2 -0
- package/dist-types/exports/markdownHook.d.ts.map +1 -0
- package/dist-types/exports/menu.d.ts +2 -0
- package/dist-types/exports/menu.d.ts.map +1 -0
- package/dist-types/exports/menus.d.ts +6 -0
- package/dist-types/exports/menus.d.ts.map +1 -0
- package/dist-types/exports/page-runtime.d.ts +2 -0
- package/dist-types/exports/page-runtime.d.ts.map +1 -0
- package/dist-types/exports/pages.d.ts +2 -0
- package/dist-types/exports/pages.d.ts.map +1 -0
- package/dist-types/exports/resolve.d.ts +2 -0
- package/dist-types/exports/resolve.d.ts.map +1 -0
- package/dist-types/exports/ssr.d.ts +2 -0
- package/dist-types/exports/ssr.d.ts.map +1 -0
- package/dist-types/exports/standalone-demo-url.d.ts +2 -0
- package/dist-types/exports/standalone-demo-url.d.ts.map +1 -0
- package/dist-types/exports/transform.d.ts +2 -0
- package/dist-types/exports/transform.d.ts.map +1 -0
- package/dist-types/exports/types/hydration.d.ts +23 -0
- package/dist-types/exports/types/hydration.d.ts.map +1 -0
- package/dist-types/exports/types/rocket.d.ts +504 -0
- package/dist-types/exports/types/rocket.d.ts.map +1 -0
- package/dist-types/exports/types.d.ts +3 -0
- package/dist-types/exports/types.d.ts.map +1 -0
- package/dist-types/exports/wds-plugin.d.ts +2 -0
- package/dist-types/exports/wds-plugin.d.ts.map +1 -0
- package/dist-types/src/PageData.d.ts +82 -0
- package/dist-types/src/PageData.d.ts.map +1 -0
- package/dist-types/src/RocketCodeBlock.d.ts +64 -0
- package/dist-types/src/RocketCodeBlock.d.ts.map +1 -0
- package/dist-types/src/RocketIcon.d.ts +35 -0
- package/dist-types/src/RocketIcon.d.ts.map +1 -0
- package/dist-types/src/RocketJsDemo.d.ts +59 -0
- package/dist-types/src/RocketJsDemo.d.ts.map +1 -0
- package/dist-types/src/RocketJsDemo.test-browser.d.ts +3 -0
- package/dist-types/src/RocketJsDemo.test-browser.d.ts.map +1 -0
- package/dist-types/src/RocketRequestDemo.d.ts +57 -0
- package/dist-types/src/RocketRequestDemo.d.ts.map +1 -0
- package/dist-types/src/RocketRequestDemo.test-browser.d.ts +3 -0
- package/dist-types/src/RocketRequestDemo.test-browser.d.ts.map +1 -0
- package/dist-types/src/SocialPreviewPlayground.d.ts +102 -0
- package/dist-types/src/SocialPreviewPlayground.d.ts.map +1 -0
- package/dist-types/src/adapters/netlify.d.ts +54 -0
- package/dist-types/src/adapters/netlify.d.ts.map +1 -0
- package/dist-types/src/asyncMessage.d.ts +14 -0
- package/dist-types/src/asyncMessage.d.ts.map +1 -0
- package/dist-types/src/cli/RocketBuild.d.ts +78 -0
- package/dist-types/src/cli/RocketBuild.d.ts.map +1 -0
- package/dist-types/src/cli/RocketCli.d.ts +17 -0
- package/dist-types/src/cli/RocketCli.d.ts.map +1 -0
- package/dist-types/src/cli/RocketInit.d.ts +22 -0
- package/dist-types/src/cli/RocketInit.d.ts.map +1 -0
- package/dist-types/src/cli/RocketStart.d.ts +45 -0
- package/dist-types/src/cli/RocketStart.d.ts.map +1 -0
- package/dist-types/src/cli/cli.d.ts +3 -0
- package/dist-types/src/cli/cli.d.ts.map +1 -0
- package/dist-types/src/component-hydration.d.ts +26 -0
- package/dist-types/src/component-hydration.d.ts.map +1 -0
- package/dist-types/src/components/FeatureList.d.ts +15 -0
- package/dist-types/src/components/FeatureList.d.ts.map +1 -0
- package/dist-types/src/components/Footer.d.ts +17 -0
- package/dist-types/src/components/Footer.d.ts.map +1 -0
- package/dist-types/src/components/Header.d.ts +6 -0
- package/dist-types/src/components/Header.d.ts.map +1 -0
- package/dist-types/src/components/RocketDrawer.d.ts +20 -0
- package/dist-types/src/components/RocketDrawer.d.ts.map +1 -0
- package/dist-types/src/components/RocketSocialLink.d.ts +30 -0
- package/dist-types/src/components/RocketSocialLink.d.ts.map +1 -0
- package/dist-types/src/components.d.ts +7 -0
- package/dist-types/src/components.d.ts.map +1 -0
- package/dist-types/src/config.d.ts +6 -0
- package/dist-types/src/config.d.ts.map +1 -0
- package/dist-types/src/debounce.d.ts +8 -0
- package/dist-types/src/debounce.d.ts.map +1 -0
- package/dist-types/src/defaultSocialPreviewTemplate.d.ts +31 -0
- package/dist-types/src/defaultSocialPreviewTemplate.d.ts.map +1 -0
- package/dist-types/src/development-page-module-loader.d.ts +15 -0
- package/dist-types/src/development-page-module-loader.d.ts.map +1 -0
- package/dist-types/src/extractCode.d.ts +5 -0
- package/dist-types/src/extractCode.d.ts.map +1 -0
- package/dist-types/src/hydration/evaluate.d.ts +20 -0
- package/dist-types/src/hydration/evaluate.d.ts.map +1 -0
- package/dist-types/src/hydration/extractStrategies.d.ts +5 -0
- package/dist-types/src/hydration/extractStrategies.d.ts.map +1 -0
- package/dist-types/src/hydration/hydrationLoader.d.ts +64 -0
- package/dist-types/src/hydration/hydrationLoader.d.ts.map +1 -0
- package/dist-types/src/icons.d.ts +182 -0
- package/dist-types/src/icons.d.ts.map +1 -0
- package/dist-types/src/layouts/atlas/atlasDocLayout.d.ts +45 -0
- package/dist-types/src/layouts/atlas/atlasDocLayout.d.ts.map +1 -0
- package/dist-types/src/layouts/atlas/atlasHeroLayout.d.ts +7 -0
- package/dist-types/src/layouts/atlas/atlasHeroLayout.d.ts.map +1 -0
- package/dist-types/src/layouts/atlas/atlasNotFoundLayout.d.ts +5 -0
- package/dist-types/src/layouts/atlas/atlasNotFoundLayout.d.ts.map +1 -0
- package/dist-types/src/layouts/layout-helper.d.ts +16 -0
- package/dist-types/src/layouts/layout-helper.d.ts.map +1 -0
- package/dist-types/src/layouts/layout.d.ts +7 -0
- package/dist-types/src/layouts/layout.d.ts.map +1 -0
- package/dist-types/src/loaded-page-module.d.ts +51 -0
- package/dist-types/src/loaded-page-module.d.ts.map +1 -0
- package/dist-types/src/main.d.ts +2 -0
- package/dist-types/src/main.d.ts.map +1 -0
- package/dist-types/src/markdownCompiler.d.ts +22 -0
- package/dist-types/src/markdownCompiler.d.ts.map +1 -0
- package/dist-types/src/markdownHook.d.ts +6 -0
- package/dist-types/src/markdownHook.d.ts.map +1 -0
- package/dist-types/src/menu.d.ts +22 -0
- package/dist-types/src/menu.d.ts.map +1 -0
- package/dist-types/src/menus/MainMenu.d.ts +23 -0
- package/dist-types/src/menus/MainMenu.d.ts.map +1 -0
- package/dist-types/src/menus/RocketMenu.d.ts +23 -0
- package/dist-types/src/menus/RocketMenu.d.ts.map +1 -0
- package/dist-types/src/menus/RocketNextPage.d.ts +18 -0
- package/dist-types/src/menus/RocketNextPage.d.ts.map +1 -0
- package/dist-types/src/menus/RocketPreviousPage.d.ts +18 -0
- package/dist-types/src/menus/RocketPreviousPage.d.ts.map +1 -0
- package/dist-types/src/menus/RocketToc.d.ts +54 -0
- package/dist-types/src/menus/RocketToc.d.ts.map +1 -0
- package/dist-types/src/menus/pageNavigation.d.ts +41 -0
- package/dist-types/src/menus/pageNavigation.d.ts.map +1 -0
- package/dist-types/src/page-pagination.d.ts +69 -0
- package/dist-types/src/page-pagination.d.ts.map +1 -0
- package/dist-types/src/page-runtime.d.ts +110 -0
- package/dist-types/src/page-runtime.d.ts.map +1 -0
- package/dist-types/src/pages.d.ts +10 -0
- package/dist-types/src/pages.d.ts.map +1 -0
- package/dist-types/src/publicAssets.d.ts +70 -0
- package/dist-types/src/publicAssets.d.ts.map +1 -0
- package/dist-types/src/requestDemoMetadata.d.ts +19 -0
- package/dist-types/src/requestDemoMetadata.d.ts.map +1 -0
- package/dist-types/src/resolve.d.ts +7 -0
- package/dist-types/src/resolve.d.ts.map +1 -0
- package/dist-types/src/siteDiscoverability.d.ts +33 -0
- package/dist-types/src/siteDiscoverability.d.ts.map +1 -0
- package/dist-types/src/siteHeadMetadata.d.ts +20 -0
- package/dist-types/src/siteHeadMetadata.d.ts.map +1 -0
- package/dist-types/src/socialPreviewImages.d.ts +186 -0
- package/dist-types/src/socialPreviewImages.d.ts.map +1 -0
- package/dist-types/src/socialPreviewTemplatePreview.d.ts +22 -0
- package/dist-types/src/socialPreviewTemplatePreview.d.ts.map +1 -0
- package/dist-types/src/ssr.d.ts +6 -0
- package/dist-types/src/ssr.d.ts.map +1 -0
- package/dist-types/src/standalone-demo-url.d.ts +60 -0
- package/dist-types/src/standalone-demo-url.d.ts.map +1 -0
- package/dist-types/src/static-page-module-loader.d.ts +15 -0
- package/dist-types/src/static-page-module-loader.d.ts.map +1 -0
- package/dist-types/src/transform.d.ts +10 -0
- package/dist-types/src/transform.d.ts.map +1 -0
- package/dist-types/src/urlLifecycle.d.ts +23 -0
- package/dist-types/src/urlLifecycle.d.ts.map +1 -0
- package/dist-types/src/wds-plugin.d.ts +15 -0
- package/dist-types/src/wds-plugin.d.ts.map +1 -0
- package/docs/assets/home-background.svg +1 -0
- package/docs/assets/prism-one-light.css +368 -0
- package/docs/assets/rocket-logo-dark-with-text-below.svg +8 -0
- package/docs/assets/rocket-logo-dark-with-text.svg +7 -0
- package/docs/assets/rocket-logo-dark.svg +7 -0
- package/docs/assets/rocket-logo-light-with-text-below.svg +14 -0
- package/docs/assets/rocket-logo-light-with-text.svg +13 -0
- package/docs/assets/rocket-logo-light.svg +12 -0
- package/docs/assets/rocket-text-no-logo.svg +3 -0
- package/exports/MainMenu.js +1 -0
- package/exports/PageData.js +1 -0
- package/exports/RocketCodeBlock.js +1 -0
- package/exports/RocketIcon.js +1 -0
- package/exports/RocketJsDemo.js +1 -0
- package/exports/RocketRequestDemo.js +1 -0
- package/exports/SocialPreviewPlayground.js +1 -0
- package/exports/adapters/netlify.js +1 -0
- package/exports/asyncMessage.js +1 -0
- package/exports/component-hydration.js +1 -0
- package/exports/components/web-awesome.js +63 -0
- package/exports/components.js +1 -0
- package/exports/config.js +1 -0
- package/exports/debounce.js +1 -0
- package/exports/define/RocketCodeBlock.js +2 -0
- package/exports/define/RocketIcon.js +3 -0
- package/exports/define/RocketJsDemo.js +5 -0
- package/exports/define/RocketRequestDemo.js +5 -0
- package/exports/define/menus.js +14 -0
- package/exports/extractCode.js +1 -0
- package/exports/globalData.js +1 -0
- package/exports/hydration/hydrationLoader.js +1 -0
- package/exports/icons.js +11 -0
- package/exports/layout-helper.js +1 -0
- package/exports/layout.js +1 -0
- package/exports/layouts/_atlas.css +3 -0
- package/exports/layouts/atlasDoc.js +5 -0
- package/exports/layouts/atlasHero.js +1 -0
- package/exports/layouts/atlasNotFound.js +4 -0
- package/exports/loaded-page-module.js +5 -0
- package/exports/markdownHook.js +4 -0
- package/exports/menu.js +4 -0
- package/exports/menus.js +5 -0
- package/exports/page-runtime.js +5 -0
- package/exports/pages.js +1 -0
- package/exports/resolve.js +1 -0
- package/exports/ssr.js +1 -0
- package/exports/standalone-demo-url.js +10 -0
- package/exports/transform.js +1 -0
- package/exports/types/hydration.ts +26 -0
- package/exports/types/rocket.ts +598 -0
- package/exports/types.ts +71 -0
- package/exports/wds-plugin.js +1 -0
- package/package.json +192 -9
- package/src/PageData.js +244 -0
- package/src/RocketCodeBlock.js +516 -0
- package/src/RocketIcon.js +291 -0
- package/src/RocketJsDemo.js +397 -0
- package/src/RocketJsDemo.test-browser.js +228 -0
- package/src/RocketRequestDemo.js +439 -0
- package/src/RocketRequestDemo.test-browser.js +301 -0
- package/src/SocialPreviewPlayground.js +573 -0
- package/src/adapters/netlify.js +814 -0
- package/src/asyncMessage.js +21 -0
- package/src/cli/RocketBuild.js +581 -0
- package/src/cli/RocketCli.js +47 -0
- package/src/cli/RocketInit.js +636 -0
- package/src/cli/RocketStart.js +145 -0
- package/src/cli/cli.js +7 -0
- package/src/component-hydration.js +86 -0
- package/src/components/FeatureList.js +114 -0
- package/src/components/Footer.js +116 -0
- package/src/components/Header.js +122 -0
- package/src/components/RocketDrawer.js +193 -0
- package/src/components/RocketSocialLink.js +128 -0
- package/src/components/assets/discord.svg +7 -0
- package/src/components/assets/github.svg +4 -0
- package/src/components/assets/gitlab.svg +1 -0
- package/src/components/assets/info.txt +1 -0
- package/src/components/assets/license.svg +3 -0
- package/src/components/assets/npm.svg +5 -0
- package/src/components/assets/slack.svg +5 -0
- package/src/components/assets/telegram.svg +4 -0
- package/src/components/assets/twitter.svg +1 -0
- package/src/components.js +34 -0
- package/src/config.js +319 -0
- package/src/debounce.js +21 -0
- package/src/defaultSocialPreviewTemplate.js +118 -0
- package/src/development-page-module-loader.js +29 -0
- package/src/extractCode.js +41 -0
- package/src/hydration/evaluate.js +54 -0
- package/src/hydration/extractStrategies.js +91 -0
- package/src/hydration/hydrationLoader.js +330 -0
- package/src/icons.js +898 -0
- package/src/layouts/atlas/atlasDoc.css +877 -0
- package/src/layouts/atlas/atlasDocLayout.js +275 -0
- package/src/layouts/atlas/atlasHero.css +774 -0
- package/src/layouts/atlas/atlasHeroLayout.js +337 -0
- package/src/layouts/atlas/atlasNotFound.css +365 -0
- package/src/layouts/atlas/atlasNotFoundLayout.js +69 -0
- package/src/layouts/layout-helper.js +92 -0
- package/src/layouts/layout.js +52 -0
- package/src/loaded-page-module.js +97 -0
- package/src/main.js +72 -0
- package/src/markdownCompiler.js +303 -0
- package/src/markdownHook.js +148 -0
- package/src/menu.js +210 -0
- package/src/menus/MainMenu.js +58 -0
- package/src/menus/RocketMenu.js +191 -0
- package/src/menus/RocketNextPage.js +25 -0
- package/src/menus/RocketPreviousPage.js +29 -0
- package/src/menus/RocketToc.js +309 -0
- package/src/menus/pageNavigation.js +285 -0
- package/src/page-pagination.js +241 -0
- package/src/page-runtime.js +481 -0
- package/src/pages.js +537 -0
- package/src/publicAssets.js +336 -0
- package/src/requestDemoMetadata.js +97 -0
- package/src/resolve.js +15 -0
- package/src/siteDiscoverability.js +184 -0
- package/src/siteHeadMetadata.js +69 -0
- package/src/socialPreviewImages.js +482 -0
- package/src/socialPreviewTemplatePreview.js +352 -0
- package/src/ssr.js +14 -0
- package/src/standalone-demo-url.js +147 -0
- package/src/static-page-module-loader.js +29 -0
- package/src/transform.js +720 -0
- package/src/urlLifecycle.js +57 -0
- package/src/wds-plugin.js +307 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import chromium from '@sparticuz/chromium';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import puppeteer from 'puppeteer-core';
|
|
6
|
+
import {
|
|
7
|
+
DEFAULT_SOCIAL_PREVIEW_IMAGE_HEIGHT,
|
|
8
|
+
DEFAULT_SOCIAL_PREVIEW_IMAGE_WIDTH,
|
|
9
|
+
defaultSocialPreviewTemplate,
|
|
10
|
+
} from './defaultSocialPreviewTemplate.js';
|
|
11
|
+
import { paginatedArchivePaths } from './page-pagination.js';
|
|
12
|
+
import { normalizeDocumentPath } from './standalone-demo-url.js';
|
|
13
|
+
import { createSiteHeadMetadata } from './siteHeadMetadata.js';
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
DEFAULT_SOCIAL_PREVIEW_IMAGE_HEIGHT,
|
|
17
|
+
DEFAULT_SOCIAL_PREVIEW_IMAGE_WIDTH,
|
|
18
|
+
defaultSocialPreviewTemplate,
|
|
19
|
+
} from './defaultSocialPreviewTemplate.js';
|
|
20
|
+
|
|
21
|
+
const LOCAL_BROWSER_ARGS = ['--no-sandbox', '--disable-setuid-sandbox'];
|
|
22
|
+
const SOCIAL_PREVIEW_RENDERER_VERSION = '1';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @typedef {{
|
|
26
|
+
* html: string;
|
|
27
|
+
* width: number;
|
|
28
|
+
* height: number;
|
|
29
|
+
* pathname: string;
|
|
30
|
+
* }} SocialPreviewCaptureOptions
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {(options: SocialPreviewCaptureOptions) => Promise<Uint8Array> | Uint8Array} SocialPreviewCapture
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @typedef {{
|
|
39
|
+
* publicPath: string;
|
|
40
|
+
* publicUrl: string;
|
|
41
|
+
* data: Uint8Array;
|
|
42
|
+
* }} DefaultSocialPreviewImageOutput
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {{
|
|
47
|
+
* pages: import('@rocket/js/types.js').PageRegistry;
|
|
48
|
+
* siteHeadMetadata?: import('@rocket/js/types.js').SiteHeadMetadataConfig;
|
|
49
|
+
* siteOrigin?: string;
|
|
50
|
+
* cacheDirectory?: string;
|
|
51
|
+
* captureSocialPreviewImage?: SocialPreviewCapture;
|
|
52
|
+
* }} options
|
|
53
|
+
* @returns {Promise<Map<string, DefaultSocialPreviewImageOutput>>}
|
|
54
|
+
*/
|
|
55
|
+
export async function generateStaticDefaultSocialPreviewImages({
|
|
56
|
+
pages,
|
|
57
|
+
siteHeadMetadata,
|
|
58
|
+
siteOrigin,
|
|
59
|
+
cacheDirectory,
|
|
60
|
+
captureSocialPreviewImage = captureSocialPreviewImageWithBrowser,
|
|
61
|
+
}) {
|
|
62
|
+
/** @type {Map<string, DefaultSocialPreviewImageOutput>} */
|
|
63
|
+
const outputs = new Map();
|
|
64
|
+
if (!siteHeadMetadata || !hasStaticSocialPreviewDelivery(siteHeadMetadata)) {
|
|
65
|
+
return outputs;
|
|
66
|
+
}
|
|
67
|
+
if (!siteOrigin) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Static Social Preview Images require a Site Origin. ` +
|
|
70
|
+
`Add siteOrigin: 'https://example.com' to rocket-config.js.`,
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
for (const [pagePath, page] of pages) {
|
|
75
|
+
if (!needsDefaultSocialPreviewImage({ pagePath, page, siteHeadMetadata })) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const { html, template, templateData } = createSocialPreviewTemplateHtml({
|
|
79
|
+
page,
|
|
80
|
+
pagePath,
|
|
81
|
+
siteHeadMetadata,
|
|
82
|
+
siteOrigin,
|
|
83
|
+
});
|
|
84
|
+
const fingerprint = createSocialPreviewImageFingerprint({
|
|
85
|
+
html,
|
|
86
|
+
pagePath,
|
|
87
|
+
siteOrigin,
|
|
88
|
+
template,
|
|
89
|
+
templateData,
|
|
90
|
+
});
|
|
91
|
+
const publicPath = defaultSocialPreviewImagePath(fingerprint);
|
|
92
|
+
const cachedData = readCachedSocialPreviewImage(cacheDirectory, fingerprint);
|
|
93
|
+
|
|
94
|
+
if (cachedData) {
|
|
95
|
+
outputs.set(pagePath, {
|
|
96
|
+
publicPath,
|
|
97
|
+
publicUrl: new URL(publicPath, `${siteOrigin}/`).href,
|
|
98
|
+
data: cachedData,
|
|
99
|
+
});
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
const data = await captureSocialPreviewImage({
|
|
105
|
+
html,
|
|
106
|
+
width: DEFAULT_SOCIAL_PREVIEW_IMAGE_WIDTH,
|
|
107
|
+
height: DEFAULT_SOCIAL_PREVIEW_IMAGE_HEIGHT,
|
|
108
|
+
pathname: pagePath,
|
|
109
|
+
});
|
|
110
|
+
outputs.set(pagePath, {
|
|
111
|
+
publicPath,
|
|
112
|
+
publicUrl: new URL(publicPath, `${siteOrigin}/`).href,
|
|
113
|
+
data,
|
|
114
|
+
});
|
|
115
|
+
writeCachedSocialPreviewImage(cacheDirectory, fingerprint, data);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Failed to capture Default Social Preview Image for ${pagePath}: ${message}`,
|
|
120
|
+
{ cause: error },
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return outputs;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {import('@rocket/js/types.js').PageRegistry} pages
|
|
129
|
+
* @returns {import('@rocket/js/types.js').PageRegistry}
|
|
130
|
+
*/
|
|
131
|
+
export function configuredConcreteSocialPreviewPages(pages) {
|
|
132
|
+
/** @type {import('@rocket/js/types.js').PageRegistry} */
|
|
133
|
+
const concretePages = new Map();
|
|
134
|
+
for (const [pagePath, page] of pages) {
|
|
135
|
+
if (hasPathParameter(pagePath)) {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
concretePages.set(pagePath, page);
|
|
139
|
+
}
|
|
140
|
+
return concretePages;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @param {import('@rocket/js/types.js').PageRegistry} pages
|
|
145
|
+
* @returns {import('@rocket/js/types.js').PageRegistry}
|
|
146
|
+
*/
|
|
147
|
+
export function publicSocialPreviewPages(pages) {
|
|
148
|
+
const concretePages = configuredConcreteSocialPreviewPages(pages);
|
|
149
|
+
/** @type {import('@rocket/js/types.js').PageRegistry} */
|
|
150
|
+
const previewPages = new Map(concretePages);
|
|
151
|
+
for (const [pagePath, page] of concretePages) {
|
|
152
|
+
for (const archivePath of paginatedArchivePaths({ pages, page, pagePath })) {
|
|
153
|
+
previewPages.set(archivePath, page);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return previewPages;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* @param {{
|
|
161
|
+
* pagePath: string;
|
|
162
|
+
* page: import('@rocket/js/types.js').Page;
|
|
163
|
+
* siteHeadMetadata?: import('@rocket/js/types.js').SiteHeadMetadataConfig;
|
|
164
|
+
* }} options
|
|
165
|
+
*/
|
|
166
|
+
export function needsDefaultSocialPreviewImage({ pagePath, page, siteHeadMetadata }) {
|
|
167
|
+
return (
|
|
168
|
+
hasStaticSocialPreviewDelivery(siteHeadMetadata) &&
|
|
169
|
+
!isFileOutputPath(pagePath) &&
|
|
170
|
+
!page.module.config.siteHeadMetadata?.socialPreview?.image
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param {import('@rocket/js/types.js').SiteHeadMetadataConfig | undefined} siteHeadMetadata
|
|
176
|
+
*/
|
|
177
|
+
export function hasStaticSocialPreviewDelivery(siteHeadMetadata) {
|
|
178
|
+
return (
|
|
179
|
+
siteHeadMetadata?.socialPreview !== undefined &&
|
|
180
|
+
(siteHeadMetadata.socialPreview.delivery ?? 'static') === 'static'
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* @param {string} pathnameOrFingerprint
|
|
186
|
+
*/
|
|
187
|
+
export function defaultSocialPreviewImagePath(pathnameOrFingerprint) {
|
|
188
|
+
if (/^[a-f0-9]{64}$/.test(pathnameOrFingerprint)) {
|
|
189
|
+
return `/_rocket/social-preview/${pathnameOrFingerprint}.png`;
|
|
190
|
+
}
|
|
191
|
+
return `/_rocket/social-preview${normalizeDocumentPath(pathnameOrFingerprint)}image.png`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @param {{
|
|
196
|
+
* html: string;
|
|
197
|
+
* pagePath: string;
|
|
198
|
+
* siteOrigin: string;
|
|
199
|
+
* template: Function;
|
|
200
|
+
* templateData: ReturnType<typeof createSocialPreviewTemplateData>;
|
|
201
|
+
* }} options
|
|
202
|
+
*/
|
|
203
|
+
function createSocialPreviewImageFingerprint({
|
|
204
|
+
html,
|
|
205
|
+
pagePath,
|
|
206
|
+
siteOrigin,
|
|
207
|
+
template,
|
|
208
|
+
templateData,
|
|
209
|
+
}) {
|
|
210
|
+
return createHash('sha256')
|
|
211
|
+
.update(
|
|
212
|
+
JSON.stringify({
|
|
213
|
+
rendererVersion: SOCIAL_PREVIEW_RENDERER_VERSION,
|
|
214
|
+
dimensions: {
|
|
215
|
+
width: DEFAULT_SOCIAL_PREVIEW_IMAGE_WIDTH,
|
|
216
|
+
height: DEFAULT_SOCIAL_PREVIEW_IMAGE_HEIGHT,
|
|
217
|
+
},
|
|
218
|
+
template:
|
|
219
|
+
template === defaultSocialPreviewTemplate ? 'rocket:default' : template.toString(),
|
|
220
|
+
templateData,
|
|
221
|
+
pagePath,
|
|
222
|
+
siteOrigin,
|
|
223
|
+
html,
|
|
224
|
+
}),
|
|
225
|
+
)
|
|
226
|
+
.digest('hex');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* @param {string | undefined} cacheDirectory
|
|
231
|
+
* @param {string} fingerprint
|
|
232
|
+
*/
|
|
233
|
+
function readCachedSocialPreviewImage(cacheDirectory, fingerprint) {
|
|
234
|
+
if (!cacheDirectory) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
const filePath = path.join(cacheDirectory, `${fingerprint}.png`);
|
|
238
|
+
if (!existsSync(filePath)) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
return readFileSync(filePath);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @param {string | undefined} cacheDirectory
|
|
246
|
+
* @param {string} fingerprint
|
|
247
|
+
* @param {Uint8Array} data
|
|
248
|
+
*/
|
|
249
|
+
function writeCachedSocialPreviewImage(cacheDirectory, fingerprint, data) {
|
|
250
|
+
if (!cacheDirectory) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
mkdirSync(cacheDirectory, { recursive: true });
|
|
254
|
+
writeFileSync(path.join(cacheDirectory, `${fingerprint}.png`), data);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @param {{
|
|
259
|
+
* page: import('@rocket/js/types.js').Page;
|
|
260
|
+
* pagePath: string;
|
|
261
|
+
* siteHeadMetadata: import('@rocket/js/types.js').SiteHeadMetadataConfig;
|
|
262
|
+
* siteOrigin: string;
|
|
263
|
+
* overrides?: { title?: string; description?: string };
|
|
264
|
+
* }} options
|
|
265
|
+
*/
|
|
266
|
+
export function createSocialPreviewTemplateHtml({
|
|
267
|
+
page,
|
|
268
|
+
pagePath,
|
|
269
|
+
siteHeadMetadata,
|
|
270
|
+
siteOrigin,
|
|
271
|
+
overrides = {},
|
|
272
|
+
}) {
|
|
273
|
+
const metadata = createSiteHeadMetadata({
|
|
274
|
+
config: siteHeadMetadata,
|
|
275
|
+
pageMetadata: page.metadata,
|
|
276
|
+
pageSiteHeadMetadata: page.module.config.siteHeadMetadata,
|
|
277
|
+
pathname: pagePath,
|
|
278
|
+
siteOrigin,
|
|
279
|
+
});
|
|
280
|
+
const template = siteHeadMetadata.socialPreview?.template ?? defaultSocialPreviewTemplate;
|
|
281
|
+
const templateData = createSocialPreviewTemplateData({
|
|
282
|
+
metadata,
|
|
283
|
+
pageMetadata: page.metadata,
|
|
284
|
+
pathname: pagePath,
|
|
285
|
+
});
|
|
286
|
+
if (overrides.title !== undefined) {
|
|
287
|
+
templateData.page.title = overrides.title;
|
|
288
|
+
templateData.page.documentTitle = overrides.title;
|
|
289
|
+
}
|
|
290
|
+
if (overrides.description !== undefined) {
|
|
291
|
+
templateData.page.description = overrides.description;
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
html: template(templateData),
|
|
295
|
+
template,
|
|
296
|
+
templateData,
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* @param {{
|
|
302
|
+
* metadata: import('@rocket/js/types.js').SiteHeadMetadata;
|
|
303
|
+
* pageMetadata?: import('@rocket/js/types.js').PageMetadata;
|
|
304
|
+
* pathname: string;
|
|
305
|
+
* }} options
|
|
306
|
+
*/
|
|
307
|
+
export function createSocialPreviewTemplateData({ metadata, pageMetadata, pathname }) {
|
|
308
|
+
return {
|
|
309
|
+
site: {
|
|
310
|
+
name: metadata.siteName,
|
|
311
|
+
defaultDescription: metadata.defaultDescription,
|
|
312
|
+
language: metadata.language,
|
|
313
|
+
...(metadata.themeColor ? { themeColor: metadata.themeColor } : {}),
|
|
314
|
+
},
|
|
315
|
+
page: {
|
|
316
|
+
pathname,
|
|
317
|
+
title: pageMetadata?.title ?? metadata.title,
|
|
318
|
+
documentTitle: metadata.title,
|
|
319
|
+
description: metadata.description,
|
|
320
|
+
canonicalUrl: metadata.canonicalUrl,
|
|
321
|
+
language: metadata.language,
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* @param {SocialPreviewCaptureOptions} options
|
|
328
|
+
*/
|
|
329
|
+
export async function captureSocialPreviewImageWithBrowser({ html }) {
|
|
330
|
+
return renderSocialPreviewImage(html);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* @param {string} html
|
|
335
|
+
* @returns {Promise<Buffer>}
|
|
336
|
+
*/
|
|
337
|
+
export async function renderSocialPreviewImage(html) {
|
|
338
|
+
/** @type {import('puppeteer-core').Browser | undefined} */
|
|
339
|
+
let browser;
|
|
340
|
+
try {
|
|
341
|
+
browser = await puppeteer.launch(await createSocialPreviewBrowserLaunchOptions());
|
|
342
|
+
const page = await browser.newPage();
|
|
343
|
+
await page.setViewport({
|
|
344
|
+
width: DEFAULT_SOCIAL_PREVIEW_IMAGE_WIDTH,
|
|
345
|
+
height: DEFAULT_SOCIAL_PREVIEW_IMAGE_HEIGHT,
|
|
346
|
+
deviceScaleFactor: 1,
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
await runSocialPreviewCaptureStep('set Social Preview HTML content', () =>
|
|
350
|
+
page.setContent(html, { waitUntil: 'load' }),
|
|
351
|
+
);
|
|
352
|
+
await runSocialPreviewCaptureStep('wait for Social Preview fonts', () =>
|
|
353
|
+
page.evaluateHandle('document.fonts.ready'),
|
|
354
|
+
);
|
|
355
|
+
const screenshot = await runSocialPreviewCaptureStep('capture Social Preview screenshot', () =>
|
|
356
|
+
page.screenshot({
|
|
357
|
+
type: 'png',
|
|
358
|
+
}),
|
|
359
|
+
);
|
|
360
|
+
return Buffer.from(screenshot);
|
|
361
|
+
} finally {
|
|
362
|
+
if (browser) {
|
|
363
|
+
await browser.close();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* @param {{
|
|
370
|
+
* platform?: NodeJS.Platform;
|
|
371
|
+
* env?: NodeJS.ProcessEnv;
|
|
372
|
+
* fileExists?: (path: string) => boolean;
|
|
373
|
+
* chromiumExecutablePath?: () => Promise<string> | string;
|
|
374
|
+
* }} [options]
|
|
375
|
+
*/
|
|
376
|
+
export async function createSocialPreviewBrowserLaunchOptions(options = {}) {
|
|
377
|
+
const platform = options.platform || process.platform;
|
|
378
|
+
return {
|
|
379
|
+
args: platform === 'linux' ? chromium.args : LOCAL_BROWSER_ARGS,
|
|
380
|
+
executablePath: await resolveSocialPreviewBrowserExecutablePath(options),
|
|
381
|
+
headless: true,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* @param {{
|
|
387
|
+
* platform?: NodeJS.Platform;
|
|
388
|
+
* env?: NodeJS.ProcessEnv;
|
|
389
|
+
* fileExists?: (path: string) => boolean;
|
|
390
|
+
* chromiumExecutablePath?: () => Promise<string> | string;
|
|
391
|
+
* }} [options]
|
|
392
|
+
*/
|
|
393
|
+
export async function resolveSocialPreviewBrowserExecutablePath({
|
|
394
|
+
platform = process.platform,
|
|
395
|
+
env = process.env,
|
|
396
|
+
fileExists = existsSync,
|
|
397
|
+
chromiumExecutablePath = () => chromium.executablePath(),
|
|
398
|
+
} = {}) {
|
|
399
|
+
const explicitPath = env.PUPPETEER_EXECUTABLE_PATH || env.CHROME_EXECUTABLE_PATH;
|
|
400
|
+
if (explicitPath) {
|
|
401
|
+
return explicitPath;
|
|
402
|
+
}
|
|
403
|
+
if (platform === 'linux') {
|
|
404
|
+
return chromiumExecutablePath();
|
|
405
|
+
}
|
|
406
|
+
const localBrowserPath = findLocalBrowserExecutable({ platform, env, fileExists });
|
|
407
|
+
if (localBrowserPath) {
|
|
408
|
+
return localBrowserPath;
|
|
409
|
+
}
|
|
410
|
+
throw new Error(
|
|
411
|
+
`Unable to find a local browser executable for Social Preview Capture. ` +
|
|
412
|
+
`Install Google Chrome or set PUPPETEER_EXECUTABLE_PATH to a Chromium-compatible browser.`,
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @param {{
|
|
418
|
+
* platform: NodeJS.Platform;
|
|
419
|
+
* env: NodeJS.ProcessEnv;
|
|
420
|
+
* fileExists: (path: string) => boolean;
|
|
421
|
+
* }} options
|
|
422
|
+
*/
|
|
423
|
+
function findLocalBrowserExecutable({ platform, env, fileExists }) {
|
|
424
|
+
const home = env.HOME || env.USERPROFILE;
|
|
425
|
+
const paths = [];
|
|
426
|
+
if (platform === 'darwin') {
|
|
427
|
+
paths.push(
|
|
428
|
+
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
|
|
429
|
+
'/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
430
|
+
'/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge',
|
|
431
|
+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser',
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
if (platform === 'win32') {
|
|
435
|
+
for (const root of [env.PROGRAMFILES, env['PROGRAMFILES(X86)'], env.LOCALAPPDATA]) {
|
|
436
|
+
if (!root) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
paths.push(
|
|
440
|
+
path.join(root, 'Google/Chrome/Application/chrome.exe'),
|
|
441
|
+
path.join(root, 'Chromium/Application/chrome.exe'),
|
|
442
|
+
path.join(root, 'Microsoft/Edge/Application/msedge.exe'),
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (home && platform === 'darwin') {
|
|
447
|
+
paths.push(
|
|
448
|
+
path.join(home, 'Applications/Google Chrome.app/Contents/MacOS/Google Chrome'),
|
|
449
|
+
path.join(home, 'Applications/Chromium.app/Contents/MacOS/Chromium'),
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
return paths.find(candidate => fileExists(candidate));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* @template T
|
|
457
|
+
* @param {string} action
|
|
458
|
+
* @param {() => Promise<T>} callback
|
|
459
|
+
* @returns {Promise<T>}
|
|
460
|
+
*/
|
|
461
|
+
async function runSocialPreviewCaptureStep(action, callback) {
|
|
462
|
+
try {
|
|
463
|
+
return await callback();
|
|
464
|
+
} catch (error) {
|
|
465
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
466
|
+
throw new Error(`Failed to ${action}: ${message}`, { cause: error });
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* @param {string} pagePath
|
|
472
|
+
*/
|
|
473
|
+
function isFileOutputPath(pagePath) {
|
|
474
|
+
return path.extname(pagePath) !== '';
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* @param {string} pagePath
|
|
479
|
+
*/
|
|
480
|
+
function hasPathParameter(pagePath) {
|
|
481
|
+
return /(^|\/):[^/]+/.test(pagePath);
|
|
482
|
+
}
|