@yolean/evidence-sdk 0.0.0-stage → 4.2.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 (186) hide show
  1. package/CHANGELOG.md +544 -0
  2. package/LICENSE.md +9 -0
  3. package/README.md +237 -2
  4. package/package.json +161 -3
  5. package/src/build-dev/cli/add/addServerHook.js +30 -0
  6. package/src/build-dev/cli/add/addTypeRef.js +19 -0
  7. package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
  8. package/src/build-dev/cli/add/updatePackageJson.js +26 -0
  9. package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
  10. package/src/build-dev/cli/add/updateViteConfig.js +152 -0
  11. package/src/build-dev/cli/add.js +95 -0
  12. package/src/build-dev/cli/build.js +30 -0
  13. package/src/build-dev/cli/dev.js +49 -0
  14. package/src/build-dev/cli/index.js +14 -0
  15. package/src/build-dev/cli/upgrade.js +54 -0
  16. package/src/build-dev/svelte/index.js +54 -0
  17. package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
  18. package/src/build-dev/svelte/metadatas.js +39 -0
  19. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
  20. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
  21. package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
  22. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
  23. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
  24. package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
  25. package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
  26. package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
  27. package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
  28. package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
  29. package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
  30. package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
  31. package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
  32. package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
  33. package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
  34. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
  35. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
  36. package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
  37. package/src/build-dev/vite/config-virtual.js +24 -0
  38. package/src/build-dev/vite/constants.js +7 -0
  39. package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
  40. package/src/build-dev/vite/evidence-config.d.ts +6 -0
  41. package/src/build-dev/vite/index.js +214 -0
  42. package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
  43. package/src/build-dev/vite/plugins.js +4 -0
  44. package/src/build-dev/vite/query-directory-hmr.js +32 -0
  45. package/src/build-dev/vite/sidecar-app.js +79 -0
  46. package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
  47. package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
  48. package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
  49. package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
  50. package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
  51. package/src/build-dev/vite/virtuals/initUsql.js +23 -0
  52. package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
  53. package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
  54. package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
  55. package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
  56. package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
  57. package/src/build-dev/vite/virtuals-dynamic.js +12 -0
  58. package/src/cli.js +5 -0
  59. package/src/cli.root.js +90 -0
  60. package/src/configuration/cli/create.js +20 -0
  61. package/src/configuration/cli/index.js +12 -0
  62. package/src/configuration/createEvidenceConfig.js +51 -0
  63. package/src/configuration/getEvidenceConfig.js +60 -0
  64. package/src/configuration/getEvidenceConfig.legacy.js +27 -0
  65. package/src/configuration/index.js +1 -0
  66. package/src/configuration/schemas/config.schema.js +11 -0
  67. package/src/configuration/schemas/deployment.schema.js +18 -0
  68. package/src/configuration/schemas/deployment.schema.spec.js +34 -0
  69. package/src/configuration/updateEvidenceConfig.js +25 -0
  70. package/src/legacy-compat.js +8 -0
  71. package/src/lib/EvidenceError.js +35 -0
  72. package/src/lib/b64Deep.js +55 -0
  73. package/src/lib/check-import.js +12 -0
  74. package/src/lib/cleanZodErrors.js +22 -0
  75. package/src/lib/cli/cmdFail.js +33 -0
  76. package/src/lib/cli/notices.js +44 -0
  77. package/src/lib/debug.js +40 -0
  78. package/src/lib/flows.d.ts +21 -0
  79. package/src/lib/forceLink.js +28 -0
  80. package/src/lib/history/index.js +70 -0
  81. package/src/lib/history/types.d.ts +8 -0
  82. package/src/lib/pkg-types.helpers.js +16 -0
  83. package/src/lib/processing-queue.js +96 -0
  84. package/src/lib/processing-queue.spec.js +148 -0
  85. package/src/lib/projectPaths.js +29 -0
  86. package/src/lib/projectRoot.js +12 -0
  87. package/src/lib/sharedPromise.js +77 -0
  88. package/src/lib/sharedPromise.spec.js +23 -0
  89. package/src/lib/strict.js +19 -0
  90. package/src/lib/tests/fs.js +21 -0
  91. package/src/lib/tokens.js +7 -0
  92. package/src/lib/unnest-zod-error.js +14 -0
  93. package/src/logger/EvidenceLogger.js +164 -0
  94. package/src/logger/index.js +4 -0
  95. package/src/meta.js +8 -0
  96. package/src/plugins/cli/index.js +13 -0
  97. package/src/plugins/cli/install.js +26 -0
  98. package/src/plugins/cli/install.prompt.js +121 -0
  99. package/src/plugins/components/getAllComponents.js +82 -0
  100. package/src/plugins/components/getComponentsInPlugin.js +33 -0
  101. package/src/plugins/components/loadComponentPlugins.js +48 -0
  102. package/src/plugins/components/loaders/file-loader.js +125 -0
  103. package/src/plugins/components/loaders/manifest-loader.js +53 -0
  104. package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
  105. package/src/plugins/components/types.d.ts +21 -0
  106. package/src/plugins/components/validateOverrides.js +30 -0
  107. package/src/plugins/components/validateOverrides.spec.js +32 -0
  108. package/src/plugins/datasources/Datasources.js +85 -0
  109. package/src/plugins/datasources/Datasources.spec.js +78 -0
  110. package/src/plugins/datasources/SourceResultCache.js +62 -0
  111. package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
  112. package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
  113. package/src/plugins/datasources/cli/edit/Options.js +253 -0
  114. package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
  115. package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
  116. package/src/plugins/datasources/cli/edit.js +20 -0
  117. package/src/plugins/datasources/cli/edit.prompt.js +100 -0
  118. package/src/plugins/datasources/cli/index.js +18 -0
  119. package/src/plugins/datasources/cli/sources.js +77 -0
  120. package/src/plugins/datasources/evalSources.js +257 -0
  121. package/src/plugins/datasources/getPublishedDatasources.js +62 -0
  122. package/src/plugins/datasources/index.js +6 -0
  123. package/src/plugins/datasources/loadSourceConfig.js +147 -0
  124. package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
  125. package/src/plugins/datasources/loadSourcePlugins.js +49 -0
  126. package/src/plugins/datasources/loadSources.js +48 -0
  127. package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
  128. package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
  129. package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
  130. package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
  131. package/src/plugins/datasources/schemas/index.js +2 -0
  132. package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
  133. package/src/plugins/datasources/schemas/results.schema.js +126 -0
  134. package/src/plugins/datasources/sub-source-vars.js +55 -0
  135. package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
  136. package/src/plugins/datasources/types.d.ts +53 -0
  137. package/src/plugins/datasources/updateManifest.js +115 -0
  138. package/src/plugins/datasources/updateManifest.spec.js +200 -0
  139. package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
  140. package/src/plugins/datasources/writeSourceConfig.js +60 -0
  141. package/src/plugins/index.js +2 -0
  142. package/src/plugins/layouts/copyMethods/index.js +10 -0
  143. package/src/plugins/layouts/copyMethods/svelte.js +53 -0
  144. package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
  145. package/src/plugins/layouts/copyToLayout.js +106 -0
  146. package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
  147. package/src/plugins/loadPluginPackage.js +77 -0
  148. package/src/plugins/schemas/index.js +1 -0
  149. package/src/plugins/schemas/plugin-config.schema.js +41 -0
  150. package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
  151. package/src/plugins/schemas/plugin-package.schema.js +154 -0
  152. package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
  153. package/src/query-builder.d.ts +1 -0
  154. package/src/query-builder.js +1 -0
  155. package/src/types/duckdb-wellknown.d.ts +35 -0
  156. package/src/types/html-attributes.ts +13 -0
  157. package/src/types/index.d.ts +18 -0
  158. package/src/types/mosaic-sql.d.ts +27 -0
  159. package/src/types/sveltekit-autoimport.d.ts +13 -0
  160. package/src/types/virtuals.d.ts +113 -0
  161. package/src/types.d.ts +1 -0
  162. package/src/usql/index.d.ts +4 -0
  163. package/src/usql/index.js +3 -0
  164. package/src/usql/query/Query.js +1391 -0
  165. package/src/usql/query/Query.spec.js +827 -0
  166. package/src/usql/query/queryScore.js +92 -0
  167. package/src/usql/query/queryScore.spec.js +26 -0
  168. package/src/usql/query/sterilizeQuery.js +89 -0
  169. package/src/usql/query/sterilizeQuery.spec.js +172 -0
  170. package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
  171. package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
  172. package/src/usql/types.ts +86 -0
  173. package/src/usql/utilities/duckdbSerialize.js +29 -0
  174. package/src/usql/utilities/index.js +2 -0
  175. package/src/usql/utilities/resolveMaybePromise.js +30 -0
  176. package/src/utils/batchUp.js +22 -0
  177. package/src/utils/batchUp.spec.js +69 -0
  178. package/src/utils/browserDebounce.js +16 -0
  179. package/src/utils/index.js +7 -0
  180. package/src/utils/svelte/addBasePath.js +74 -0
  181. package/src/utils/svelte/addBasePath.spec.js +108 -0
  182. package/src/utils/svelte/index.js +13 -0
  183. package/src/utils/svelte/inputs.js +138 -0
  184. package/tsconfig.json +22 -0
  185. package/vite.config.js +3 -0
  186. package/vitest.config.js +15 -0
@@ -0,0 +1,29 @@
1
+ import { getEvidenceConfig } from '../../../../configuration/getEvidenceConfig.js';
2
+ import { addBasePath } from '../../../../utils/svelte/addBasePath.js';
3
+
4
+ /** @type {import("@evidence-dev/sdk/config").EvidenceConfig} */
5
+ let cfg;
6
+
7
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
8
+ export const addBasePathToHrefAndSrc = {
9
+ markup: ({ content, filename }) => {
10
+ if (!filename?.endsWith('+page.md')) return;
11
+ if (!cfg) {
12
+ cfg = getEvidenceConfig();
13
+ }
14
+ let r = content;
15
+
16
+ const regex = /(href|src)=['"]?([^'">= ]*)['"]?/g;
17
+ const matches = content.matchAll(regex);
18
+ for (const match of matches) {
19
+ const originalContent = match[0];
20
+ const path = match[2];
21
+ if (path) {
22
+ const newPath = addBasePath(path, cfg);
23
+ r = r.replace(originalContent, originalContent.replace(path, newPath));
24
+ }
25
+ }
26
+
27
+ return { code: r };
28
+ }
29
+ };
@@ -0,0 +1,82 @@
1
+ import { describe, expect, it, vi } from 'vitest';
2
+ /** @type {undefined | string} */
3
+ let basePath = undefined;
4
+
5
+ vi.mock('$evidence/config', () => ({
6
+ config: {
7
+ deployment: {
8
+ get basePath() {
9
+ return basePath;
10
+ }
11
+ }
12
+ }
13
+ }));
14
+ vi.mock('../../../../configuration/getEvidenceConfig.js', () => ({
15
+ getEvidenceConfig() {
16
+ return {
17
+ deployment: {
18
+ get basePath() {
19
+ return basePath;
20
+ }
21
+ }
22
+ };
23
+ }
24
+ }));
25
+
26
+ import { addBasePathToHrefAndSrc } from './addBasePathToHrefAndSrc.js';
27
+ describe('addBasePathToHrefAndSrc', () => {
28
+ it('should leave files without href/src untouched', () => {
29
+ const content = '<div>test</div>';
30
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(content);
31
+ });
32
+ it('should add the base path to a relative link', () => {
33
+ basePath = '/base';
34
+ const content = '<a href="/test">test</a><img src="/test">test</img>';
35
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
36
+ '<a href="/base/test">test</a><img src="/base/test">test</img>'
37
+ );
38
+ });
39
+ it('should add the base path to multiple relative paths', () => {
40
+ basePath = '/base';
41
+ const content =
42
+ '<a href="/test">test</a><a href="/test2">test2</a><img src="/test">test</img><img src="/test2">test2</img>';
43
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
44
+ '<a href="/base/test">test</a><a href="/base/test2">test2</a><img src="/base/test">test</img><img src="/base/test2">test2</img>'
45
+ );
46
+ });
47
+ it('should ignore absolute paths', () => {
48
+ basePath = '/base';
49
+ const content =
50
+ '<a href="https://example.com/test">test</a><img src="https://example.com/test">test</img>';
51
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(content);
52
+ });
53
+ it('should properly handle a mix of absolute and relative paths', () => {
54
+ basePath = '/base';
55
+ const content =
56
+ '<a href="https://example.com/test">test</a><a href="/test">test</a><img src="https://example.com/test">test</img><img src="/test">test</img>';
57
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
58
+ '<a href="https://example.com/test">test</a><a href="/base/test">test</a><img src="https://example.com/test">test</img><img src="/base/test">test</img>'
59
+ );
60
+ });
61
+ it('should work on paths in single quotes', () => {
62
+ basePath = '/base';
63
+ const content = "<a href='/test'>test</a><img src='/test'>test</img>";
64
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
65
+ "<a href='/base/test'>test</a><img src='/base/test'>test</img>"
66
+ );
67
+ });
68
+ it('should work on paths in double quotes', () => {
69
+ basePath = '/base';
70
+ const content = '<a href="/test">test</a><img src="/test">test</img>';
71
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
72
+ '<a href="/base/test">test</a><img src="/base/test">test</img>'
73
+ );
74
+ });
75
+ it('should work on paths with no quotes', () => {
76
+ basePath = '/base';
77
+ const content = '<a href=/test>test</a><img src=/test>test</img>';
78
+ expect(addBasePathToHrefAndSrc.markup({ content, filename: '+page.md' }).code).toBe(
79
+ '<a href=/base/test>test</a><img src=/base/test>test</img>'
80
+ );
81
+ });
82
+ });
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @type {import("svelte/types/compiler/preprocess").PreprocessorGroup}
3
+ */
4
+ export const addScriptTags = {
5
+ markup({ content }) {
6
+ // Make sure that we have script tags; without this we won't be able to preprocess them
7
+ if (!content.match(/<script(.*)>/)) {
8
+ return { code: '<script context="module"> </script>' + '<script> </script>' + content };
9
+ }
10
+ if (!content.match(/<script(.*)context="module"(.*)>/)) {
11
+ return { code: '<script context="module"> </script>' + content };
12
+ }
13
+ if (!content.match(/<script(.*)>/)) {
14
+ return { code: '<script> </script>' + content };
15
+ }
16
+ }
17
+ };
@@ -0,0 +1,19 @@
1
+ import { processMarkdown } from './processMarkdown.js';
2
+
3
+ /**
4
+ * @type {import("svelte/types/compiler/preprocess").PreprocessorGroup}
5
+ */
6
+ export const convertToHtml = {
7
+ markup: async ({ filename, content }) => {
8
+ /** @type {string} */
9
+ let htmlContent = content;
10
+
11
+ if (filename?.endsWith('.md')) {
12
+ htmlContent = await processMarkdown(content, filename);
13
+ }
14
+
15
+ // TODO: Other languages?
16
+
17
+ return { code: htmlContent };
18
+ }
19
+ };
@@ -0,0 +1,123 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { convertToHtml } from './convertToHtml.js';
3
+
4
+ describe('convertToHtml', () => {
5
+ describe('Basic Elements', () => {
6
+ it.each([
7
+ { label: 'h1', html: '<h1>%copy</h1>', markdown: '# %copy' },
8
+ { label: 'h2', html: '<h2>%copy</h2>', markdown: '## %copy' },
9
+ { label: 'h3', html: '<h3>%copy</h3>', markdown: '### %copy' },
10
+ { label: 'h4', html: '<h4>%copy</h4>', markdown: '#### %copy' },
11
+ { label: 'h5', html: '<h5>%copy</h5>', markdown: '##### %copy' },
12
+ { label: 'h6', html: '<h6>%copy</h6>', markdown: '###### %copy' },
13
+ { label: 'italics as <p><em>', html: '<p><em>%copy</em></p>', markdown: '_%copy_' },
14
+ { label: 'italics as <p><em>', html: '<p><em>%copy</em></p>', markdown: '*%copy*' },
15
+ {
16
+ label: 'inline italics',
17
+ html: '<p>%copy <em>%copy</em> %copy</p>',
18
+ markdown: '%copy _%copy_ %copy'
19
+ },
20
+ {
21
+ label: 'inline italics',
22
+ html: '<p>%copy <em>%copy</em> %copy</p>',
23
+ markdown: '%copy *%copy* %copy'
24
+ },
25
+ // prettier-ignore
26
+ { label: 'bold as <p><strong>', html: '<p><strong>%copy</strong></p>', markdown: '__%copy__' },
27
+ /* TODO: Inline Bold */
28
+ // prettier-ignore
29
+ { label: 'bold as <p><strong>', html: '<p><strong>%copy</strong></p>', markdown: '**%copy**' },
30
+ // prettier-ignore
31
+ { label: 'blockquote', html: '<blockquote>\n<p>%copy</p>\n</blockquote>', markdown: '> %copy' },
32
+ // prettier-ignore
33
+ { label: 'links', html: `<p><a href="%link" rel="nofollow">%copy</a></p>`, markdown: "[%copy](%link)"},
34
+ // prettier-ignore
35
+ { label: 'images', html: `<p><img src="%link" alt="%copy"></p>`, markdown: "![%copy](%link)"},
36
+ { label: 'paragraphs', html: '<p>%copy</p>', markdown: '%copy' },
37
+ { label: 'ul', html: `<ul>\n<li>Input</li>\n</ul>`, markdown: '- %copy' },
38
+ { label: 'ol', html: `<ol>\n<li>Input</li>\n</ol>`, markdown: '1. %copy' },
39
+ { label: 'hr', html: '<hr>', markdown: '---' },
40
+ { label: 'hr', html: '<hr>', markdown: '***' }
41
+ /* TODO: Nested Lists */
42
+ /* TODO: Tables */
43
+ /* TODO: Code - this is very very important */
44
+ ])('Creates $label with $markdown', async (opts) => {
45
+ const transform = (s) =>
46
+ s.replaceAll('%copy', 'Input').replaceAll('%link', 'https://example.com');
47
+
48
+ const result = await convertToHtml.markup({
49
+ filename: '+page.md',
50
+ content: transform(opts.markdown)
51
+ });
52
+ expect(result.code).toEqual(transform(opts.html));
53
+ });
54
+ });
55
+
56
+ describe('Svelte', () => {
57
+ describe('html', () => {
58
+ it('Should leave Svelte Components untouched', async () => {
59
+ const r = await convertToHtml.markup({
60
+ filename: '+page.svelte',
61
+ content: `<Query>SELECT 1</Query>`
62
+ });
63
+ expect(r.code).toEqual(`<Query>SELECT 1</Query>`);
64
+ });
65
+ it('Should leave {#if} untouched (html)', async () => {
66
+ const r = await convertToHtml.markup({
67
+ filename: '+page.svelte',
68
+ content: `{#if true} <p>Hi</p> {/if}`
69
+ });
70
+ expect(r.code).toEqual(`{#if true} <p>Hi</p> {/if}`);
71
+ });
72
+ it('Should not break with on: directives', async () => {
73
+ const r = await convertToHtml.markup({
74
+ filename: '+page.svelte',
75
+ content: `<button on:click={console.log}></button>`
76
+ });
77
+ expect(r.code).toEqual(`<button on:click={console.log}></button>`);
78
+ });
79
+ });
80
+ describe('markdown', () => {
81
+ it('Should leave Svelte Components untouched', async () => {
82
+ const r = await convertToHtml.markup({
83
+ filename: '+page.md',
84
+ content: `<Query>SELECT 1</Query>`
85
+ });
86
+ expect(r.code).toEqual(`<Query>SELECT 1</Query>`);
87
+ });
88
+
89
+ it('Should leave {#if} untouched', async () => {
90
+ const r = await convertToHtml.markup({
91
+ filename: '+page.md',
92
+ content: `
93
+ {#if true}
94
+ Hi
95
+ {/if}`
96
+ });
97
+ expect(r.code).toEqual(`{#if true}\n<p>Hi\n{/if}</p>`);
98
+ });
99
+ });
100
+ });
101
+
102
+ describe('Query Conversion', () => {
103
+ it('Should convert queries to html', async () => {
104
+ const r = await convertToHtml.markup({
105
+ filename: '+page.md',
106
+ content: `\`\`\`sql query_name
107
+ SELECT * FROM users
108
+ \`\`\`
109
+ `
110
+ });
111
+ expect(r.code).toBe(
112
+ `<pre><code class="language-sql" lang="sql" evidence-query-name="query_name">SELECT * FROM users</code></pre>`
113
+ );
114
+ });
115
+ });
116
+
117
+ describe('Tables', () => {
118
+ it('Should handle them properly', () => {});
119
+ });
120
+ describe('Frontmatter', () => {
121
+ it('Should not include frontmatter in output HTML', async () => {});
122
+ });
123
+ });
@@ -0,0 +1,59 @@
1
+ import { JSDOM } from 'jsdom';
2
+ import chalk from 'chalk';
3
+ import * as mdx from 'mdsvex';
4
+
5
+ /**
6
+ *
7
+ * @param {string} filename
8
+ * @returns {Exclude<mdx.MdsvexCompileOptions["highlight"], false | undefined>["highlighter"]}
9
+ */
10
+ const highlighter =
11
+ (filename) =>
12
+ /**
13
+ *
14
+ * @param {string} code
15
+ * @param {string} [lang]
16
+ * @param {string} [meta]
17
+ * @returns {string}
18
+ */
19
+
20
+ (code, lang, meta) => {
21
+ const fragment = JSDOM.fragment('<pre><code></code></pre>');
22
+ const preEl = fragment.querySelector('pre');
23
+ const codeEl = fragment.querySelector('code');
24
+ if (!preEl) throw new Error('<pre/> Element not found (internal error)');
25
+ if (!codeEl) throw new Error('<code/> Element not found (internal error)');
26
+
27
+ if (lang) {
28
+ codeEl.classList.add(`language-${lang}`);
29
+ }
30
+ if (lang !== 'sql' || (lang === 'sql' && !meta)) {
31
+ if (lang) codeEl.setAttribute('lang', lang);
32
+ if (meta) codeEl.setAttribute('data-meta', meta);
33
+ } else {
34
+ if (!meta) {
35
+ // TODO: This never actually fires because the behavior for bare sql needs to be defined
36
+ console.log(chalk.red.bold(`[!] Query with no name detected in ${filename}!`));
37
+ console.log(chalk.red(`\t${code.split('\n')[0]}`));
38
+ }
39
+ codeEl.setAttribute('lang', lang);
40
+ if (meta) codeEl.setAttribute('evidence-query-name', meta);
41
+ }
42
+ codeEl.textContent = code;
43
+ return preEl.outerHTML;
44
+ };
45
+
46
+ /**
47
+ * @param {string} content
48
+ * @param {string} filename
49
+ * @returns {Promise<string>}
50
+ */
51
+ export const processMarkdown = async (content, filename) => {
52
+ const mdxResult = await mdx.compile(content, {
53
+ smartypants: false,
54
+ highlight: { highlighter: highlighter(filename) }
55
+ });
56
+
57
+ if (!mdxResult) throw new Error(`${filename} | Failed to convert markdown to html`);
58
+ return mdxResult.code.trim();
59
+ };
@@ -0,0 +1,49 @@
1
+ import { setFileMetadata } from '../../metadatas.js';
2
+ import * as jsdom from 'jsdom';
3
+
4
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
5
+ export const extractQueries = {
6
+ markup: ({ filename, content }) => {
7
+ if (!filename) return;
8
+ const dom = new jsdom.JSDOM(content, { contentType: 'text/html' });
9
+ const codeElements = dom.window.document.querySelectorAll('code');
10
+ /** @type {import('../../metadatas.js').FileMetadata} */
11
+ const data = { queries: {} };
12
+ // TODO: Is this actually needed?
13
+ // const body = dom.window.document.documentElement.querySelector('body');
14
+ // if (!body) throw new Error('Body is undefined');
15
+ // dom.window.document.head.querySelectorAll('script').forEach((el) => body.prepend(el));
16
+ let r = content;
17
+
18
+ codeElements.forEach((el) => {
19
+ if (el.getAttribute('evidence-query-name') && !el.getAttribute('lang'))
20
+ el.setAttribute('lang', 'sql');
21
+ const lang =
22
+ el.attributes.getNamedItem('lang')?.value ??
23
+ el.attributes.getNamedItem('data-lang')?.value ??
24
+ Array.from(el.classList.values())
25
+ .find((cn) => cn.startsWith('language-'))
26
+ ?.split('language-')[1];
27
+
28
+ const content = el.textContent ?? '';
29
+ const queryName = el.attributes.getNamedItem('evidence-query-name')?.value;
30
+ if (queryName) {
31
+ switch (lang) {
32
+ case 'sql':
33
+ data.queries[queryName] = content.trim();
34
+ break;
35
+ case 'malloy':
36
+ // TODO: Convert malloy to sql
37
+ break;
38
+ case 'prql':
39
+ // TODO: Convert prql to sql
40
+ break;
41
+ }
42
+ }
43
+ });
44
+
45
+ setFileMetadata(filename, data);
46
+
47
+ return { code: r.trim() };
48
+ }
49
+ };
@@ -0,0 +1,54 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { extractQueries } from './extractQueries.js';
3
+ import { clearFileMetadatas, getFileMetadata } from '../../metadatas.js';
4
+
5
+ describe('extractQueries', () => {
6
+ beforeEach(() => {
7
+ clearFileMetadatas();
8
+ });
9
+ it('Should extract a single query', () => {
10
+ const queryContent = `SELECT * FROM users`;
11
+ const filename = `+page.svelte`;
12
+ const content = `<code lang="sql" evidence-query-name="simple_query">${queryContent}</code>`;
13
+
14
+ extractQueries.markup({ filename, content });
15
+ expect(getFileMetadata(filename).queries).toHaveProperty('simple_query');
16
+ expect(getFileMetadata(filename).queries.simple_query).toEqual(queryContent);
17
+ });
18
+ it('Should infer sql lang when query name is present', () => {
19
+ const queryContent = `SELECT * FROM users`;
20
+ const filename = `+page.svelte`;
21
+ const content = `<code evidence-query-name="simple_query">${queryContent}</code>`;
22
+
23
+ extractQueries.markup({ filename, content });
24
+ expect(getFileMetadata(filename).queries).toHaveProperty('simple_query');
25
+ expect(getFileMetadata(filename).queries.simple_query).toEqual(queryContent);
26
+ });
27
+ it('Should extract multiple queries', () => {
28
+ const queryContent = [`SELECT * FROM users`, `SELECT * FROM posts`];
29
+ const filename = `+page.svelte`;
30
+ const content = queryContent
31
+ .map(
32
+ (q, i) => `
33
+ <code lang="sql" evidence-query-name="simple_query_${i}">${q}</code>
34
+ `
35
+ )
36
+ .join('\n');
37
+
38
+ extractQueries.markup({ filename, content });
39
+ let i = 0;
40
+ for (const query of queryContent) {
41
+ expect(getFileMetadata(filename).queries).toHaveProperty(`simple_query_${i}`);
42
+ expect(getFileMetadata(filename).queries[`simple_query_${i}`]).toEqual(query);
43
+ i++;
44
+ }
45
+ });
46
+ it('Should ignore code blocks without a query name', () => {
47
+ const queryContent = `SELECT * FROM users`;
48
+ const filename = `+page.svelte`;
49
+ const content = `<code lang="sql">${queryContent}</code>`;
50
+
51
+ extractQueries.markup({ filename, content });
52
+ expect(getFileMetadata(filename).queries).not.toHaveProperty('simple_query');
53
+ });
54
+ });
@@ -0,0 +1,52 @@
1
+ import * as jsdom from 'jsdom';
2
+ import hljs from 'highlight.js';
3
+
4
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
5
+ export const highlight = {
6
+ markup: ({ content }) => {
7
+ const dom = new jsdom.JSDOM(content, { contentType: 'text/html' });
8
+ const codeElements = dom.window.document.querySelectorAll('code');
9
+ let r = content;
10
+
11
+ codeElements.forEach((el) => {
12
+ const originalContent = el.outerHTML;
13
+ if (el.getAttribute('evidence-query-name') && !el.getAttribute('lang'))
14
+ el.setAttribute('lang', 'sql');
15
+
16
+ const lang =
17
+ el.attributes.getNamedItem('lang')?.value ??
18
+ el.attributes.getNamedItem('data-lang')?.value ??
19
+ Array.from(el.classList.values())
20
+ .find((cn) => cn.startsWith('language-'))
21
+ ?.split('language-')[1];
22
+
23
+ if (!lang) return;
24
+ const elContent = el.textContent ?? '';
25
+
26
+ const highlighted =
27
+ lang && hljs.listLanguages().includes(lang.toLowerCase())
28
+ ? hljs.highlight(elContent, { language: lang.toLowerCase() })
29
+ : elContent;
30
+ if (lang) el.classList.add(`language-${lang}`);
31
+ if (['javascript', 'js'].includes(lang)) el.removeAttribute('lang');
32
+
33
+ if (typeof highlighted === 'string') el.innerHTML = highlighted;
34
+ else el.innerHTML = highlighted.value;
35
+
36
+ const doc = el.ownerDocument;
37
+ let target = el;
38
+
39
+ if (el.parentElement && el.parentElement.tagName.toLowerCase() !== 'pre') {
40
+ const pre = doc.createElement('pre');
41
+ el.parentElement.insertBefore(pre, el);
42
+ el.remove();
43
+ pre.appendChild(el);
44
+ target = pre;
45
+ }
46
+
47
+ r = r.replace(originalContent, target.outerHTML);
48
+ });
49
+
50
+ return { code: r };
51
+ }
52
+ };
@@ -0,0 +1,31 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { highlight } from './highlight.js';
3
+
4
+ describe('highlight', () => {
5
+ it('Should highlight javascript', () => {
6
+ const c = highlight.markup({
7
+ content: '<code lang="javascript">console.log(`Hello World!`)</code>'
8
+ });
9
+ expect(c.code).toEqual(
10
+ `<pre><code class="language-javascript"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">\`Hello World!\`</span>)</code></pre>`
11
+ );
12
+ });
13
+ it('Should highlight SQL', () => {
14
+ const c = highlight.markup({ content: '<code lang="sql">SELECT * FROM world</code>' });
15
+ expect(c.code).toEqual(
16
+ `<pre><code lang="sql" class="language-sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> world</code></pre>`
17
+ );
18
+ });
19
+ it('Should not add a pre to a code block already wrapped in pre', () => {
20
+ const c = highlight.markup({
21
+ content: '<pre><code lang="sql">SELECT * FROM world</code></pre>'
22
+ });
23
+ expect(c.code).toEqual(
24
+ `<pre><code lang="sql" class="language-sql"><span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> world</code></pre>`
25
+ );
26
+ });
27
+ it('Should not highlight code blocks without a language', () => {
28
+ const c = highlight.markup({ content: '<code>SELECT * FROM world</code>' });
29
+ expect(c.code).toEqual(`<code>SELECT * FROM world</code>`);
30
+ });
31
+ });
@@ -0,0 +1,15 @@
1
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
2
+ export const initUniversalSql = {
3
+ script: ({ filename, content, attributes }) => {
4
+ // TODO: How does this map with non-sveltekit projects?
5
+ if (filename?.endsWith('.svelte-kit/generated/root.svelte') && !attributes.context) {
6
+ return {
7
+ code: `
8
+ import "$evidence/initUsql"
9
+ import bootstrapContexts from "$evidence/bootstrapContexts.svelte.js"
10
+ bootstrapContexts()
11
+ ${content}`
12
+ };
13
+ }
14
+ }
15
+ };
@@ -0,0 +1,56 @@
1
+ import chalk from 'chalk';
2
+ import { getAllComponents } from '../../../../plugins/components/getAllComponents.js';
3
+ import autoImport from 'sveltekit-autoimport';
4
+
5
+ /**
6
+ * @type {() => import("svelte/types/compiler/preprocess").PreprocessorGroup}
7
+ */
8
+ export const injectComponents = () => {
9
+ const componentPlugins = getAllComponents();
10
+
11
+ const packages = componentPlugins
12
+ .then((components) => {
13
+ /** @type {Record<string,string[]>} */
14
+ const packs = {};
15
+ for (const [component, data] of Object.entries(components)) {
16
+ if (!packs[data.name]) packs[data.name] = [];
17
+ const import_name = data.aliasOf ? `${data.aliasOf} as ${component}` : component;
18
+ packs[data.name].push(import_name);
19
+ }
20
+ return packs;
21
+ })
22
+ .catch((e) => {
23
+ console.warn(chalk.yellow('Failed to load component plugins'), e);
24
+ return {};
25
+ });
26
+
27
+ const autoImporter = packages.then((packages) =>
28
+ autoImport({
29
+ include: ['**/*.(svelte|md)'],
30
+ module: packages,
31
+ components: [{ directory: '../../components', flat: true }]
32
+ })
33
+ );
34
+
35
+ return {
36
+ /** @type {import("svelte/types/compiler/preprocess").MarkupPreprocessor}} */
37
+ markup: async ({ content, filename }) => {
38
+ if (filename?.endsWith('.svelte')) return; /* Don't autoimport into svelte files */
39
+ const components = await componentPlugins.catch((e) => {
40
+ console.debug(e);
41
+ return false;
42
+ });
43
+ if (!components) {
44
+ return;
45
+ }
46
+
47
+ const { markup: autoimport_process_markup } = await autoImporter;
48
+
49
+ return autoimport_process_markup({ content, filename });
50
+ },
51
+ /** @type {import("svelte/types/compiler/preprocess").Preprocessor}} */
52
+ style: async () => {},
53
+ /** @type {import("svelte/types/compiler/preprocess").Preprocessor}} */
54
+ script: async () => {}
55
+ };
56
+ };
@@ -0,0 +1,14 @@
1
+ /** @type {import("svelte/types/compiler/preprocess").PreprocessorGroup} */
2
+ export const injectHighlightStyles = {
3
+ script: ({ filename, content, attributes }) => {
4
+ // TODO: How does this map with non-sveltekit projects?
5
+ // Maybe we need a <Evidence/> component
6
+ // Could we do this with Vite?
7
+ if (filename?.endsWith('.svelte-kit/generated/root.svelte') && !attributes.context)
8
+ return {
9
+ code: `
10
+ import 'highlight.js/styles/hybrid.css';
11
+ ${content}`
12
+ };
13
+ }
14
+ };