@jaamd/jaad 0.0.0-stage → 0.11.0

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.
Files changed (89) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/dist/jaad.css +1446 -0
  4. package/index.ts +45 -0
  5. package/package.json +70 -3
  6. package/site.ts +19 -0
  7. package/src/@types/docs.ts +47 -0
  8. package/src/advanced.ts +21 -0
  9. package/src/collection.ts +97 -0
  10. package/src/components/Footer.astro +18 -0
  11. package/src/components/Head.astro +119 -0
  12. package/src/components/Header.astro +47 -0
  13. package/src/components/LocaleSwitcher.astro +111 -0
  14. package/src/components/Nav.astro +22 -0
  15. package/src/components/NavMobile.astro +105 -0
  16. package/src/components/ScrollToTop.astro +43 -0
  17. package/src/components/SearchPalette.astro +328 -0
  18. package/src/components/SocialLinks.astro +27 -0
  19. package/src/components/ThemeSwitcher.astro +55 -0
  20. package/src/components/docs/Breadcrumbs.astro +36 -0
  21. package/src/components/docs/DocsMobileNav.astro +93 -0
  22. package/src/components/docs/DocsPage.astro +68 -0
  23. package/src/components/docs/DocsPageNav.astro +53 -0
  24. package/src/components/docs/DocsSidebarLeft.astro +82 -0
  25. package/src/components/docs/PageActions.astro +81 -0
  26. package/src/components/docs/StructuredData.astro +55 -0
  27. package/src/components/docs/TableOfContents.astro +73 -0
  28. package/src/components/locale-switcher.ts +6 -0
  29. package/src/config.ts +236 -0
  30. package/src/content.ts +42 -0
  31. package/src/docs-config.ts +30 -0
  32. package/src/env.d.ts +13 -0
  33. package/src/fonts/OFL.txt +94 -0
  34. package/src/fonts/inter.woff2 +0 -0
  35. package/src/fonts/merriweather-italic.woff2 +0 -0
  36. package/src/fonts/merriweather.woff2 +0 -0
  37. package/src/fonts.ts +29 -0
  38. package/src/forges.ts +60 -0
  39. package/src/infer.ts +75 -0
  40. package/src/integration.ts +270 -0
  41. package/src/layouts/DefaultDocsFrame.astro +31 -0
  42. package/src/layouts/Docs.astro +168 -0
  43. package/src/layouts/Page.astro +38 -0
  44. package/src/locale-tree.ts +51 -0
  45. package/src/locales.ts +191 -0
  46. package/src/navigation.ts +97 -0
  47. package/src/routes/404.astro +16 -0
  48. package/src/routes/docs-index.astro +19 -0
  49. package/src/routes/docs-slug.astro +58 -0
  50. package/src/routes/docs-slug.md.ts +37 -0
  51. package/src/routes/llms-locale.txt.ts +8 -0
  52. package/src/routes/llms.txt.ts +4 -0
  53. package/src/routes/search-index-locale.json.ts +8 -0
  54. package/src/routes/search-index.json.ts +4 -0
  55. package/src/social.ts +44 -0
  56. package/src/styles/components/breadcrumbs.css +13 -0
  57. package/src/styles/components/docs-mobile-nav.css +9 -0
  58. package/src/styles/components/docs-page-nav.css +31 -0
  59. package/src/styles/components/docs-page.css +9 -0
  60. package/src/styles/components/docs-sidebar.css +42 -0
  61. package/src/styles/components/footer.css +8 -0
  62. package/src/styles/components/header.css +20 -0
  63. package/src/styles/components/locale-switcher.css +20 -0
  64. package/src/styles/components/nav-mobile.css +15 -0
  65. package/src/styles/components/nav.css +11 -0
  66. package/src/styles/components/not-found.css +11 -0
  67. package/src/styles/components/page-actions.css +13 -0
  68. package/src/styles/components/scroll-to-top.css +11 -0
  69. package/src/styles/components/search.css +98 -0
  70. package/src/styles/components/shell.css +73 -0
  71. package/src/styles/components/social-links.css +8 -0
  72. package/src/styles/components/theme-switcher.css +14 -0
  73. package/src/styles/components/toc.css +21 -0
  74. package/src/styles/docs.css +112 -0
  75. package/src/styles/global.css +162 -0
  76. package/src/styles/jaamd-forward.css +16 -0
  77. package/src/styles/jaamd-reverse.css +18 -0
  78. package/src/styles/jaamd.css +11 -0
  79. package/src/styles/tailwind.css +30 -0
  80. package/src/themes/index.ts +26 -0
  81. package/src/ui/en.ts +29 -0
  82. package/src/ui/it.ts +30 -0
  83. package/src/ui.ts +49 -0
  84. package/src/urls.ts +49 -0
  85. package/src/utils/docs.ts +300 -0
  86. package/src/utils/endpoints.ts +80 -0
  87. package/src/utils/helpers.ts +4 -0
  88. package/src/utils/search.ts +83 -0
  89. package/src/virtual.ts +48 -0
package/src/content.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { glob } from 'astro/loaders'
2
+ import { z } from 'astro/zod'
3
+ import { defineCollection } from 'astro:content'
4
+ import config from 'virtual:jaad/config'
5
+ import { getDocCollectionId } from './utils/docs.ts'
6
+
7
+ const docsPages = defineCollection({
8
+ loader: glob({
9
+ pattern: '**/*.md',
10
+ base: config.docsDir,
11
+ // Keep URLs file-driven even if markdown contains an unrelated slug field.
12
+ generateId: ({ entry }) => getDocCollectionId(entry),
13
+ }),
14
+ // Every field is optional: markdown without frontmatter works as-is.
15
+ schema: z.object({
16
+ title: z.string().optional(),
17
+ description: z.string().optional(),
18
+ /** Shorter label, in navigation only. */
19
+ label: z.string().optional(),
20
+ /** Pairs this page with its translations when the slugs differ. */
21
+ translationKey: z.string().optional(),
22
+ /** Replaces the filename prefix number. */
23
+ order: z.number().int().optional(),
24
+ /** Left out of production builds. */
25
+ draft: z.boolean().default(false),
26
+ /** Metadata and search weight; never rendered. */
27
+ keywords: z.array(z.string()).default([]),
28
+ author: z.string().optional(),
29
+ /** Public path or URL; false drops the site's. */
30
+ ogImage: z
31
+ .union([
32
+ z.string().refine((v) => v.startsWith('/') || /^https?:\/\//.test(v), {
33
+ message: 'ogImage must be a public url, such as "/og/page.png"',
34
+ }),
35
+ z.literal(false),
36
+ ])
37
+ .optional(),
38
+ lastUpdated: z.coerce.date().optional(),
39
+ }),
40
+ })
41
+
42
+ export const collections = { docsPages }
@@ -0,0 +1,30 @@
1
+ import type { Locale } from './locales.ts'
2
+ import type { DocsUrlOptions } from './urls.ts'
3
+ import config from 'virtual:jaad/config'
4
+ import * as locales from './locales.ts'
5
+ import { uiString, type UiKey } from './ui.ts'
6
+
7
+ /** The virtual config, read once. */
8
+ export const LOCALES: Locale[] = config.docsLocales
9
+ export const DEFAULT_LOCALE: string = config.defaultLocale
10
+
11
+ /** The four fields every href builder takes, in one place. */
12
+ export function docsUrls(locale?: string): DocsUrlOptions {
13
+ return {
14
+ docsBase: config.docsBase,
15
+ deploymentBase: import.meta.env.BASE_URL,
16
+ locale,
17
+ defaultLocale: DEFAULT_LOCALE,
18
+ }
19
+ }
20
+
21
+ export const localePrefix = (locale?: string) => locales.localePrefix(locale, DEFAULT_LOCALE)
22
+
23
+ export const buildLocales = () => locales.routedLocales(LOCALES)
24
+
25
+ export const prefixedCodes = () => locales.prefixedLocales(LOCALES, DEFAULT_LOCALE)
26
+
27
+ /** The interface strings for a page, already resolved. */
28
+ export function ui(locale: string = DEFAULT_LOCALE) {
29
+ return (key: UiKey) => uiString(config.ui, locale, key)
30
+ }
package/src/env.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /// <reference types="astro/client" />
2
+
3
+ declare module 'virtual:jaad/config' {
4
+ // oxlint-disable-next-line typescript/consistent-type-imports
5
+ const config: import('./config.ts').JaadResolvedConfig
6
+ export default config
7
+ }
8
+
9
+ declare module 'virtual:jaad/docs-frame' {
10
+ // oxlint-disable-next-line typescript/consistent-type-imports
11
+ const DocsFrame: typeof import('./layouts/DefaultDocsFrame.astro').default
12
+ export default DocsFrame
13
+ }
@@ -0,0 +1,94 @@
1
+ Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter)
2
+ Copyright 2020 The Merriweather Project Authors (https://github.com/EbenSorkin/Merriweather4) with Reserved Font Name "Merriweather".
3
+
4
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
5
+ This license is copied below, and is also available with a FAQ at:
6
+ https://scripts.sil.org/OFL
7
+
8
+
9
+ -----------------------------------------------------------
10
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
11
+ -----------------------------------------------------------
12
+
13
+ PREAMBLE
14
+ The goals of the Open Font License (OFL) are to stimulate worldwide
15
+ development of collaborative font projects, to support the font creation
16
+ efforts of academic and linguistic communities, and to provide a free and
17
+ open framework in which fonts may be shared and improved in partnership
18
+ with others.
19
+
20
+ The OFL allows the licensed fonts to be used, studied, modified and
21
+ redistributed freely as long as they are not sold by themselves. The
22
+ fonts, including any derivative works, can be bundled, embedded,
23
+ redistributed and/or sold with any software provided that any reserved
24
+ names are not used by derivative works. The fonts and derivatives,
25
+ however, cannot be released under any other type of license. The
26
+ requirement for fonts to remain under this license does not apply
27
+ to any document created using the fonts or their derivatives.
28
+
29
+ DEFINITIONS
30
+ "Font Software" refers to the set of files released by the Copyright
31
+ Holder(s) under this license and clearly marked as such. This may
32
+ include source files, build scripts and documentation.
33
+
34
+ "Reserved Font Name" refers to any names specified as such after the
35
+ copyright statement(s).
36
+
37
+ "Original Version" refers to the collection of Font Software components as
38
+ distributed by the Copyright Holder(s).
39
+
40
+ "Modified Version" refers to any derivative made by adding to, deleting,
41
+ or substituting -- in part or in whole -- any of the components of the
42
+ Original Version, by changing formats or by porting the Font Software to a
43
+ new environment.
44
+
45
+ "Author" refers to any designer, engineer, programmer, technical
46
+ writer or other person who contributed to the Font Software.
47
+
48
+ PERMISSION & CONDITIONS
49
+ Permission is hereby granted, free of charge, to any person obtaining
50
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
51
+ redistribute, and sell modified and unmodified copies of the Font
52
+ Software, subject to the following conditions:
53
+
54
+ 1) Neither the Font Software nor any of its individual components,
55
+ in Original or Modified Versions, may be sold by itself.
56
+
57
+ 2) Original or Modified Versions of the Font Software may be bundled,
58
+ redistributed and/or sold with any software, provided that each copy
59
+ contains the above copyright notice and this license. These can be
60
+ included either as stand-alone text files, human-readable headers or
61
+ in the appropriate machine-readable metadata fields within text or
62
+ binary files as long as those fields can be easily viewed by the user.
63
+
64
+ 3) No Modified Version of the Font Software may use the Reserved Font
65
+ Name(s) unless explicit written permission is granted by the corresponding
66
+ Copyright Holder. This restriction only applies to the primary font name as
67
+ presented to the users.
68
+
69
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
70
+ Software shall not be used to promote, endorse or advertise any
71
+ Modified Version, except to acknowledge the contribution(s) of the
72
+ Copyright Holder(s) and the Author(s) or with their explicit written
73
+ permission.
74
+
75
+ 5) The Font Software, modified or unmodified, in part or in whole,
76
+ must be distributed entirely under this license, and must not be
77
+ distributed under any other license. The requirement for fonts to
78
+ remain under this license does not apply to any document created
79
+ using the Font Software.
80
+
81
+ TERMINATION
82
+ This license becomes null and void if any of the above conditions are
83
+ not met.
84
+
85
+ DISCLAIMER
86
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
89
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
90
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
91
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
92
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
93
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
94
+ OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file
Binary file
package/src/fonts.ts ADDED
@@ -0,0 +1,29 @@
1
+ import { fontProviders } from 'astro/config'
2
+
3
+ const file = (name: string) => new URL(`./fonts/${name}.woff2`, import.meta.url)
4
+
5
+ export const fonts = [
6
+ {
7
+ provider: fontProviders.local(),
8
+ name: 'Inter',
9
+ cssVariable: '--font-inter',
10
+ options: {
11
+ variants: [{ weight: '400 600', style: 'normal', src: [file('inter')] }],
12
+ },
13
+ },
14
+ {
15
+ provider: fontProviders.local(),
16
+ name: 'Merriweather',
17
+ cssVariable: '--font-merriweather',
18
+ options: {
19
+ variants: [
20
+ { weight: '300 700', style: 'normal', src: [file('merriweather')] },
21
+ {
22
+ weight: '300 700',
23
+ style: 'italic',
24
+ src: [file('merriweather-italic')],
25
+ },
26
+ ],
27
+ },
28
+ },
29
+ ]
package/src/forges.ts ADDED
@@ -0,0 +1,60 @@
1
+ // Icon paths from simple-icons (CC0-1.0). Only git forges are bundled: they
2
+ // are what the repository link inferred from the git remote needs.
3
+
4
+ interface Forge {
5
+ label: string
6
+ /** Single 24×24 filled path. */
7
+ path: string
8
+ /** Host that identifies this forge in a git remote, when unambiguous. */
9
+ host: string | null
10
+ }
11
+
12
+ export const FORGES: Record<string, Forge> = {
13
+ github: {
14
+ label: 'GitHub',
15
+ host: 'github.com',
16
+ path: 'M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12',
17
+ },
18
+ gitlab: {
19
+ label: 'GitLab',
20
+ host: 'gitlab.com',
21
+ path: 'm23.6004 9.5927-.0337-.0862L20.3.9814a.851.851 0 0 0-.3362-.405.8748.8748 0 0 0-.9997.0539.8748.8748 0 0 0-.29.4399l-2.2055 6.748H7.5375l-2.2057-6.748a.8573.8573 0 0 0-.29-.4412.8748.8748 0 0 0-.9997-.0537.8585.8585 0 0 0-.3362.4049L.4332 9.5015l-.0325.0862a6.0657 6.0657 0 0 0 2.0119 7.0105l.0113.0087.03.0213 4.976 3.7264 2.462 1.8633 1.4995 1.1321a1.0085 1.0085 0 0 0 1.2197 0l1.4995-1.1321 2.4619-1.8633 5.006-3.7489.0125-.01a6.0682 6.0682 0 0 0 2.0094-7.003z',
22
+ },
23
+ codeberg: {
24
+ label: 'Codeberg',
25
+ host: 'codeberg.org',
26
+ path: 'M11.999.747A11.974 11.974 0 0 0 0 12.75c0 2.254.635 4.465 1.833 6.376L11.837 6.19c.072-.092.251-.092.323 0l4.178 5.402h-2.992l.065.239h3.113l.882 1.138h-3.674l.103.374h3.86l.777 1.003h-4.358l.135.483h4.593l.695.894h-5.038l.165.589h5.326l.609.785h-5.717l.182.65h6.038l.562.727h-6.397l.183.65h6.717A12.003 12.003 0 0 0 24 12.75 11.977 11.977 0 0 0 11.999.747zm3.654 19.104.182.65h5.326c.173-.204.353-.433.513-.65zm.385 1.377.18.65h3.563c.233-.198.485-.428.712-.65zm.383 1.377.182.648h1.203c.356-.204.685-.412 1.042-.648z',
27
+ },
28
+ gitea: {
29
+ label: 'Gitea',
30
+ host: 'gitea.com',
31
+ path: 'M4.209 4.603c-.247 0-.525.02-.84.088-.333.07-1.28.283-2.054 1.027C-.403 7.25.035 9.685.089 10.052c.065.446.263 1.687 1.21 2.768 1.749 2.141 5.513 2.092 5.513 2.092s.462 1.103 1.168 2.119c.955 1.263 1.936 2.248 2.89 2.367 2.406 0 7.212-.004 7.212-.004s.458.004 1.08-.394c.535-.324 1.013-.893 1.013-.893s.492-.527 1.18-1.73c.21-.37.385-.729.538-1.068 0 0 2.107-4.471 2.107-8.823-.042-1.318-.367-1.55-.443-1.627-.156-.156-.366-.153-.366-.153s-4.475.252-6.792.306c-.508.011-1.012.023-1.512.027v4.474l-.634-.301c0-1.39-.004-4.17-.004-4.17-1.107.016-3.405-.084-3.405-.084s-5.399-.27-5.987-.324c-.187-.011-.401-.032-.648-.032zm.354 1.832h.111s.271 2.269.6 3.597C5.549 11.147 6.22 13 6.22 13s-.996-.119-1.641-.348c-.99-.324-1.409-.714-1.409-.714s-.73-.511-1.096-1.52C1.444 8.73 2.021 7.7 2.021 7.7s.32-.859 1.47-1.145c.395-.106.863-.12 1.072-.12zm8.33 2.554c.26.003.509.127.509.127l.868.422-.529 1.075a.686.686 0 0 0-.614.359.685.685 0 0 0 .072.756l-.939 1.924a.69.69 0 0 0-.66.527.687.687 0 0 0 .347.763.686.686 0 0 0 .867-.206.688.688 0 0 0-.069-.882l.916-1.874a.667.667 0 0 0 .237-.02.657.657 0 0 0 .271-.137 8.826 8.826 0 0 1 1.016.512.761.761 0 0 1 .286.282c.073.21-.073.569-.073.569-.087.29-.702 1.55-.702 1.55a.692.692 0 0 0-.676.477.681.681 0 1 0 1.157-.252c.073-.141.141-.282.214-.431.19-.397.515-1.16.515-1.16.035-.066.218-.394.103-.814-.095-.435-.48-.638-.48-.638-.467-.301-1.116-.58-1.116-.58s0-.156-.042-.27a.688.688 0 0 0-.148-.241l.516-1.062 2.89 1.401s.48.218.583.619c.073.282-.019.534-.069.657-.24.587-2.1 4.317-2.1 4.317s-.232.554-.748.588a1.065 1.065 0 0 1-.393-.045l-.202-.08-4.31-2.1s-.417-.218-.49-.596c-.083-.31.104-.691.104-.691l2.073-4.272s.183-.37.466-.497a.855.855 0 0 1 .35-.077z',
32
+ },
33
+ forgejo: {
34
+ label: 'Forgejo',
35
+ host: null,
36
+ path: 'M16.7773 0c1.6018 0 2.9004 1.2986 2.9004 2.9005s-1.2986 2.9004-2.9004 2.9004c-1.0854 0-2.0315-.596-2.5288-1.4787H12.91c-2.3322 0-4.2272 1.8718-4.2649 4.195l-.0007 2.1175a7.0759 7.0759 0 0 1 4.148-1.4205l.1176-.001 1.3385.0002c.4973-.8827 1.4434-1.4788 2.5288-1.4788 1.6018 0 2.9004 1.2986 2.9004 2.9005s-1.2986 2.9004-2.9004 2.9004c-1.0854 0-2.0315-.596-2.5288-1.4787H12.91c-2.3322 0-4.2272 1.8718-4.2649 4.195l-.0007 2.319c.8827.4973 1.4788 1.4434 1.4788 2.5287 0 1.602-1.2986 2.9005-2.9005 2.9005-1.6018 0-2.9004-1.2986-2.9004-2.9005 0-1.0853.596-2.0314 1.4788-2.5287l-.0002-9.9831c0-3.887 3.1195-7.0453 6.9915-7.108l.1176-.001h1.3385C14.7458.5962 15.692 0 16.7773 0ZM7.2227 19.9052c-.6596 0-1.1943.5347-1.1943 1.1943s.5347 1.1943 1.1943 1.1943 1.1944-.5347 1.1944-1.1943-.5348-1.1943-1.1944-1.1943Zm9.5546-10.4644c-.6596 0-1.1944.5347-1.1944 1.1943s.5348 1.1943 1.1944 1.1943c.6596 0 1.1943-.5347 1.1943-1.1943s-.5347-1.1943-1.1943-1.1943Zm0-7.7346c-.6596 0-1.1944.5347-1.1944 1.1943s.5348 1.1943 1.1944 1.1943c.6596 0 1.1943-.5347 1.1943-1.1943s-.5347-1.1943-1.1943-1.1943Z',
37
+ },
38
+ bitbucket: {
39
+ label: 'Bitbucket',
40
+ host: 'bitbucket.org',
41
+ path: 'M.778 1.213a.768.768 0 00-.768.892l3.263 19.81c.084.5.515.868 1.022.873H19.95a.772.772 0 00.77-.646l3.27-20.03a.768.768 0 00-.768-.891zM14.52 15.53H9.522L8.17 8.466h7.561z',
42
+ },
43
+ sourcehut: {
44
+ label: 'SourceHut',
45
+ host: 'sr.ht',
46
+ path: 'M12 0C5.371 0 0 5.371 0 12s5.371 12 12 12 12-5.371 12-12S18.629 0 12 0Zm0 21.677A9.675 9.675 0 0 1 2.323 12 9.675 9.675 0 0 1 12 2.323 9.675 9.675 0 0 1 21.677 12 9.675 9.675 0 0 1 12 21.677Z',
47
+ },
48
+ }
49
+
50
+ /** Matches a repository URL to a bundled forge icon, if we recognise the host. */
51
+ export function forgeForUrl(url: string): string | null {
52
+ const host = url.match(/^https?:\/\/([^/]+)/)?.[1]
53
+ if (!host) return null
54
+ for (const [name, forge] of Object.entries(FORGES)) {
55
+ if (forge.host && (host === forge.host || host.endsWith('.' + forge.host))) {
56
+ return name
57
+ }
58
+ }
59
+ return null
60
+ }
package/src/infer.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { execFileSync } from 'node:child_process'
2
+ import { readFileSync } from 'node:fs'
3
+ import { join } from 'node:path'
4
+
5
+ const git = (args: string[], cwd: string): string | null => {
6
+ try {
7
+ return execFileSync('git', args, {
8
+ cwd,
9
+ encoding: 'utf8',
10
+ stdio: ['ignore', 'pipe', 'ignore'],
11
+ }).trim()
12
+ } catch {
13
+ return null
14
+ }
15
+ }
16
+
17
+ /** `git@host:owner/repo.git` and `https://host/owner/repo.git` both become
18
+ * `https://host/owner/repo`. */
19
+ export function normaliseRemote(remote: string): string | null {
20
+ const ssh = remote.match(/^[\w.-]+@([\w.-]+):(.+?)(?:\.git)?$/)
21
+ if (ssh) return `https://${ssh[1]}/${ssh[2]}`
22
+
23
+ const https = remote.match(/^https?:\/\/(?:[^@/]+@)?([\w.-]+)\/(.+?)(?:\.git)?$/)
24
+ if (https) return `https://${https[1]}/${https[2]}`
25
+
26
+ return null
27
+ }
28
+
29
+ export interface RepoInfo {
30
+ /** Browsable base, e.g. https://github.com/owner/repo */
31
+ url: string
32
+ branch: string
33
+ /** Path from the repository root to the Astro project, "" at the root. */
34
+ prefix: string
35
+ }
36
+
37
+ /** Returns null outside a repository, in a tarball, or without an origin. */
38
+ export function inferRepo(cwd: string = process.cwd()): RepoInfo | null {
39
+ const remote = git(['remote', 'get-url', 'origin'], cwd)
40
+ if (!remote) return null
41
+
42
+ const url = normaliseRemote(remote)
43
+ if (!url) return null
44
+
45
+ return {
46
+ url,
47
+ branch: git(['rev-parse', '--abbrev-ref', 'HEAD'], cwd) || 'main',
48
+ prefix: git(['rev-parse', '--show-prefix'], cwd) ?? '',
49
+ }
50
+ }
51
+
52
+ /** Ends in a slash. `docsDir` resolves against the repository root, not the
53
+ * Astro project, so a site in a subdirectory still links correctly. */
54
+ export function editBaseFrom(repo: RepoInfo, docsDir: string): string {
55
+ const dir = docsDir.replace(/^\.\//, '').replace(/\/$/, '')
56
+ const path = `${repo.prefix}${dir}`.replace(/^\/+/, '')
57
+ return `${repo.url}/edit/${repo.branch}/${path}/`
58
+ }
59
+
60
+ /** `:path` is substituted when present, otherwise the file is appended. */
61
+ export function editUrl(base: string, filePath: string): string {
62
+ return base.includes(':path') ? base.replace(':path', filePath) : base.replace(/\/?$/, '/') + filePath
63
+ }
64
+
65
+ export function inferDescription(cwd: string = process.cwd()): string | null {
66
+ try {
67
+ const pkg = JSON.parse(readFileSync(join(cwd, 'package.json'), 'utf8')) as {
68
+ description?: unknown
69
+ }
70
+
71
+ return typeof pkg.description === 'string' && pkg.description ? pkg.description : null
72
+ } catch {
73
+ return null
74
+ }
75
+ }
@@ -0,0 +1,270 @@
1
+ import type { JaadResolvedConfig, resolveStylesheets } from './config.ts'
2
+ import type { AstroIntegration, AstroUserConfig } from 'astro'
3
+ import { existsSync, readFileSync, readdirSync } from 'node:fs'
4
+ import { createRequire } from 'node:module'
5
+ import { join, relative, resolve } from 'node:path'
6
+ import { fileURLToPath, pathToFileURL } from 'node:url'
7
+ import { fonts } from './fonts.ts'
8
+ import { prefixedLocales } from './locales.ts'
9
+ import { docsPageHref, normaliseBasePath, type DocsUrlOptions } from './urls.ts'
10
+ import { getCleanSlug, getDocCollectionId, sortDocPages, splitDocLocale } from './utils/docs.ts'
11
+ import { jaadVirtualPlugin } from './virtual.ts'
12
+
13
+ type Stylesheets = ReturnType<typeof resolveStylesheets>
14
+ type VitePlugins = NonNullable<NonNullable<AstroUserConfig['vite']>['plugins']>
15
+
16
+ const NOT_INSTALLED = new Set(['MODULE_NOT_FOUND', 'ERR_PACKAGE_PATH_NOT_EXPORTED'])
17
+
18
+ /** The site's own `@tailwindcss/vite`, unless it already registered one. */
19
+ export function loadTailwindPlugin(root: string, registered: VitePlugins | undefined): VitePlugins {
20
+ const plugins = ([registered] as unknown[]).flat(Infinity) as ({
21
+ name?: string
22
+ } | null)[]
23
+ if (plugins.some((p) => p?.name?.startsWith('@tailwindcss/vite'))) return []
24
+
25
+ const load = createRequire(join(root, 'package.json'))
26
+ let tailwindcss: () => VitePlugins
27
+ try {
28
+ load.resolve('tailwindcss/package.json')
29
+ ;({ default: tailwindcss } = load('@tailwindcss/vite') as {
30
+ default: () => VitePlugins
31
+ })
32
+ } catch (error) {
33
+ if (!NOT_INSTALLED.has((error as { code?: string }).code ?? '')) throw error
34
+ throw new Error(
35
+ 'jaad: `tailwind: true` needs Tailwind in your project:\n npm i -D tailwindcss @tailwindcss/vite',
36
+ { cause: error },
37
+ )
38
+ }
39
+ return tailwindcss()
40
+ }
41
+
42
+ /** One per locale: each has its own first page, and its own redirect. */
43
+ export interface OpeningPage {
44
+ locale?: string
45
+ slug: string
46
+ }
47
+
48
+ export interface IntegrationState {
49
+ deploymentBase: string
50
+ openingPages: OpeningPage[]
51
+ docsFrame: string
52
+ }
53
+
54
+ const INDEX_PAGE_NAMES = ['index.astro', 'index.md', 'index.mdx', 'index.html', 'index.js', 'index.ts']
55
+
56
+ const NOT_FOUND_PAGE_NAMES = ['404.astro', '404.md', '404.mdx', '404.html', '404.js', '404.ts']
57
+
58
+ const routeEntrypoint = (file: string) => fileURLToPath(new URL(`./routes/${file}`, import.meta.url))
59
+
60
+ export function shouldInjectNotFound(docsBase: string, srcDir: URL): boolean {
61
+ return docsBase === '' && !NOT_FOUND_PAGE_NAMES.some((name) => existsSync(new URL(`pages/${name}`, srcDir)))
62
+ }
63
+
64
+ /** The two fields that move the opening page. The content collection is not
65
+ * available this early, so `order` and `draft` are read from the file. */
66
+ function readFrontmatter(source: string): { order?: number; draft?: boolean } {
67
+ const block = /^---\r?\n([\s\S]*?)\r?\n---/.exec(source)
68
+ if (!block) return {}
69
+
70
+ const order = /^order:\s*(-?\d+)\s*$/m.exec(block[1])
71
+ const draft = /^draft:\s*(true|false)\s*$/m.exec(block[1])
72
+ return {
73
+ order: order ? Number(order[1]) : undefined,
74
+ draft: draft ? draft[1] === 'true' : undefined,
75
+ }
76
+ }
77
+
78
+ export function findOpeningPages(dir: string, locales: string[] = []): OpeningPage[] {
79
+ if (!existsSync(dir)) return []
80
+
81
+ const pages: {
82
+ id: string
83
+ localeId: string
84
+ locale: string | null
85
+ data: { order?: number }
86
+ }[] = []
87
+
88
+ const visit = (current: string) => {
89
+ for (const entry of readdirSync(current, { withFileTypes: true }).toSorted((a, b) =>
90
+ a.name.localeCompare(b.name),
91
+ )) {
92
+ const file = join(current, entry.name)
93
+ if (entry.isDirectory()) visit(file)
94
+ else if (entry.isFile() && entry.name.endsWith('.md')) {
95
+ const front = readFrontmatter(readFileSync(file, 'utf8'))
96
+ if (front.draft) continue
97
+
98
+ const id = getDocCollectionId(relative(dir, file))
99
+ const split = splitDocLocale(id, locales)
100
+ pages.push({
101
+ id,
102
+ localeId: split.id,
103
+ locale: split.locale,
104
+ data: { order: front.order },
105
+ })
106
+ }
107
+ }
108
+ }
109
+
110
+ visit(dir)
111
+
112
+ const groups = locales.length > 0 ? locales : [null]
113
+ return groups.flatMap((locale) => {
114
+ const first = sortDocPages(pages.filter((page) => page.locale === locale))[0]
115
+ // Indexed access is typed as always present; a locale can have no pages.
116
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
117
+ if (!first) return []
118
+ return [{ locale: locale ?? undefined, slug: getCleanSlug(first.localeId) }]
119
+ })
120
+ }
121
+
122
+ export function getInjectedRoutes(docsBase: string, locales: string[] = []): [string, string][] {
123
+ // `[...slug]` already absorbs "it/guide" and "it". Only the two fixed
124
+ // endpoints need a route of their own, and `[locale]` hands them the code.
125
+ const perLocale: [string, string][] =
126
+ locales.length > 0
127
+ ? [
128
+ [`${docsBase}/[locale]/search-index.json`, 'search-index-locale.json.ts'],
129
+ [`${docsBase}/[locale]/llms.txt`, 'llms-locale.txt.ts'],
130
+ ]
131
+ : []
132
+
133
+ return [
134
+ [docsBase, 'docs-index.astro'],
135
+ [`${docsBase}/search-index.json`, 'search-index.json.ts'],
136
+ [`${docsBase}/llms.txt`, 'llms.txt.ts'],
137
+ ...perLocale,
138
+ [`${docsBase}/[...slug]`, 'docs-slug.astro'],
139
+ [`${docsBase}/[...slug].md`, 'docs-slug.md.ts'],
140
+ ]
141
+ }
142
+
143
+ export function createIntegrationState(): IntegrationState {
144
+ return {
145
+ deploymentBase: '',
146
+ openingPages: [],
147
+ docsFrame: fileURLToPath(new URL('./layouts/DefaultDocsFrame.astro', import.meta.url)),
148
+ }
149
+ }
150
+
151
+ export function createCoreIntegration(
152
+ config: JaadResolvedConfig,
153
+ stylesheets: Stylesheets,
154
+ state: IntegrationState,
155
+ discoverOpeningPage: boolean,
156
+ ): AstroIntegration {
157
+ return {
158
+ name: 'jaad',
159
+ hooks: {
160
+ 'astro:config:setup': ({ config: astroConfig, updateConfig, injectRoute, addWatchFile, logger }) => {
161
+ updateConfig({
162
+ vite: {
163
+ plugins: [
164
+ jaadVirtualPlugin(
165
+ config,
166
+ stylesheets.user,
167
+ stylesheets.theme,
168
+ stylesheets.bridge,
169
+ stylesheets.tailwind,
170
+ () => state.docsFrame,
171
+ ),
172
+ ...(config.tailwind
173
+ ? [loadTailwindPlugin(fileURLToPath(astroConfig.root), astroConfig.vite.plugins)]
174
+ : []),
175
+ ],
176
+ // Package .astro sources must reach the Astro compiler.
177
+ ssr: { noExternal: ['@jaamd/jaad'] },
178
+ },
179
+ fonts,
180
+ })
181
+
182
+ for (const [pattern, entrypoint] of getInjectedRoutes(
183
+ config.docsBase,
184
+ prefixedLocales(config.docsLocales, config.defaultLocale),
185
+ )) {
186
+ injectRoute({
187
+ pattern,
188
+ entrypoint: routeEntrypoint(entrypoint),
189
+ prerender: true,
190
+ })
191
+ }
192
+
193
+ const customDocsFrame = new URL('jaad/DocsFrame.astro', astroConfig.srcDir)
194
+ if (existsSync(customDocsFrame)) {
195
+ state.docsFrame = fileURLToPath(customDocsFrame)
196
+ addWatchFile(customDocsFrame)
197
+ }
198
+
199
+ if (shouldInjectNotFound(config.docsBase, astroConfig.srcDir)) {
200
+ injectRoute({
201
+ pattern: '/404',
202
+ entrypoint: routeEntrypoint('404.astro'),
203
+ prerender: true,
204
+ })
205
+ }
206
+
207
+ const docsDirPath = resolve(fileURLToPath(astroConfig.root), config.docsDir)
208
+ const docsDir = pathToFileURL(docsDirPath)
209
+ if (!existsSync(docsDir)) {
210
+ logger.warn(
211
+ `${config.docsDir} does not exist, so the site has no pages. ` +
212
+ 'Create it, or point docsDir at your markdown.',
213
+ )
214
+ } else if (discoverOpeningPage) {
215
+ state.openingPages = findOpeningPages(
216
+ docsDirPath,
217
+ config.docsLocales.map((locale) => locale.code),
218
+ )
219
+ }
220
+ addWatchFile(docsDir)
221
+
222
+ if (config.docsBase === '') {
223
+ const ownIndex = INDEX_PAGE_NAMES.find((name) =>
224
+ existsSync(new URL(`pages/${name}`, astroConfig.srcDir)),
225
+ )
226
+ if (ownIndex) {
227
+ logger.warn(
228
+ `${ownIndex} owns the site root while JAAD docs are mounted there. ` +
229
+ 'Set routeBase: "/docs" to keep a landing page and mount the docs below it.',
230
+ )
231
+ }
232
+ }
233
+ },
234
+
235
+ 'astro:config:done': ({ config: astroConfig, injectTypes }) => {
236
+ state.deploymentBase = normaliseBasePath(astroConfig.base)
237
+ injectTypes({
238
+ filename: 'jaad.d.ts',
239
+ content: `declare module "virtual:jaad/config" {
240
+ const config: import("@jaamd/jaad").JaadResolvedConfig;
241
+ export default config;
242
+ }`,
243
+ })
244
+ },
245
+ },
246
+ }
247
+ }
248
+
249
+ const withoutTrailingSlash = (value: string) => (value.length > 1 ? value.replace(/\/$/, '') : value)
250
+
251
+ export function createSitemapFilter(
252
+ docsBase: string,
253
+ state: Pick<IntegrationState, 'deploymentBase' | 'openingPages'>,
254
+ defaultLocale?: string,
255
+ ): (page: string) => boolean {
256
+ return (page) => {
257
+ if (state.openingPages.length === 0) return true
258
+
259
+ const pathname = withoutTrailingSlash(new URL(page).pathname)
260
+ return !state.openingPages.some((opening) => {
261
+ const urls: DocsUrlOptions = {
262
+ docsBase,
263
+ deploymentBase: state.deploymentBase,
264
+ locale: opening.locale,
265
+ defaultLocale,
266
+ }
267
+ return pathname === withoutTrailingSlash(docsPageHref(opening.slug, urls))
268
+ })
269
+ }
270
+ }
@@ -0,0 +1,31 @@
1
+ ---
2
+ import type { DocsFrameProps } from '../advanced.ts'
3
+
4
+ type Props = DocsFrameProps
5
+ ---
6
+
7
+ <div data-jaad-default-frame class="docs-frame">
8
+ <slot name="header" />
9
+
10
+ <div class="docs-container">
11
+ <slot name="sidebar" />
12
+
13
+ <div class="docs-main docs-main-gutters">
14
+ <main class="docs-page-main">
15
+ <slot name="mobile-navigation" />
16
+
17
+ <article>
18
+ <slot name="breadcrumbs" />
19
+ <slot name="page-actions" />
20
+ <slot name="content" />
21
+ </article>
22
+
23
+ <slot name="page-navigation" />
24
+ </main>
25
+ </div>
26
+
27
+ <slot name="table-of-contents" />
28
+ </div>
29
+
30
+ <slot name="footer" />
31
+ </div>