@jsonresume/core 0.2.0 → 0.3.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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@jsonresume/core",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Framework-agnostic resume component core - design tokens, primitives, and validators",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
7
7
  "exports": {
8
8
  ".": "./src/index.js",
9
+ "./ssr": "./src/ssr/index.js",
9
10
  "./helpers": "./src/helpers/index.js",
10
11
  "./tokens": "./src/tokens/index.js",
11
12
  "./primitives": "./src/primitives/index.js",
@@ -35,8 +36,8 @@
35
36
  },
36
37
  "dependencies": {
37
38
  "styled-components": "^6.1.19",
38
- "@jsonresume/types": "0.2.0",
39
- "@jsonresume/utils": "0.2.0"
39
+ "@jsonresume/types": "0.2.1",
40
+ "@jsonresume/utils": "0.2.1"
40
41
  },
41
42
  "keywords": [
42
43
  "resume",
@@ -0,0 +1,142 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import React from 'react';
3
+ import styled from 'styled-components';
4
+ import { renderResumeDocument, googleFontsLinks } from '../../ssr/index.js';
5
+
6
+ const Box = styled.div`
7
+ color: rgb(1, 2, 3);
8
+ `;
9
+
10
+ describe('googleFontsLinks', () => {
11
+ it('returns empty string for no fonts', () => {
12
+ expect(googleFontsLinks()).toBe('');
13
+ expect(googleFontsLinks([])).toBe('');
14
+ expect(googleFontsLinks(null)).toBe('');
15
+ });
16
+
17
+ it('builds a single css2 request from family names', () => {
18
+ const out = googleFontsLinks(['Inter', 'IBM Plex Sans']);
19
+ expect(out).toContain('rel="preconnect"');
20
+ expect(out).toContain('fonts.gstatic.com');
21
+ expect(out).toContain(
22
+ 'https://fonts.googleapis.com/css2?family=Inter&family=IBM+Plex+Sans&display=swap'
23
+ );
24
+ });
25
+
26
+ it('preserves axis/weight specs on a family name', () => {
27
+ const out = googleFontsLinks(['Inter:wght@400;700']);
28
+ expect(out).toContain('family=Inter:wght@400;700');
29
+ });
30
+
31
+ it('passes through a ready-made href', () => {
32
+ const href = 'https://fonts.googleapis.com/css2?family=Roboto&display=swap';
33
+ const out = googleFontsLinks([href]);
34
+ expect(out).toContain(`<link href="${href}" rel="stylesheet">`);
35
+ // No css2 family= request was synthesised on top of the href.
36
+ expect(out).not.toContain('family=Roboto&family');
37
+ });
38
+
39
+ it('passes through a ready-made <link> tag verbatim', () => {
40
+ const tag = '<link href="https://example.com/f.css" rel="stylesheet">';
41
+ expect(googleFontsLinks([tag])).toContain(tag);
42
+ });
43
+ });
44
+
45
+ describe('renderResumeDocument', () => {
46
+ it('emits a doctype, the element markup, and the collected styles', () => {
47
+ const out = renderResumeDocument(<Box>Hello</Box>);
48
+ expect(out.startsWith('<!DOCTYPE html>')).toBe(true);
49
+ expect(out).toContain('Hello');
50
+ // styled-components injected a <style> tag with the declared color.
51
+ expect(out).toContain('<style');
52
+ expect(out).toContain('color:rgb(1, 2, 3)');
53
+ expect(out.trimEnd().endsWith('</html>')).toBe(true);
54
+ });
55
+
56
+ it('includes the font links when fonts are given', () => {
57
+ const out = renderResumeDocument(<Box>Hi</Box>, { fonts: ['Inter'] });
58
+ expect(out).toContain(
59
+ 'https://fonts.googleapis.com/css2?family=Inter&display=swap'
60
+ );
61
+ });
62
+
63
+ it('links the @jsonresume/core tokens.css by default', () => {
64
+ const out = renderResumeDocument(<Box>Hi</Box>);
65
+ expect(out).toContain('@jsonresume/core/dist/tokens.css');
66
+ });
67
+
68
+ it('omits tokens.css when includeTokensCss is false', () => {
69
+ const out = renderResumeDocument(<Box>Hi</Box>, {
70
+ includeTokensCss: false,
71
+ });
72
+ expect(out).not.toContain('tokens.css');
73
+ });
74
+
75
+ it('honours title, lang and dir options', () => {
76
+ const out = renderResumeDocument(<Box>Hi</Box>, {
77
+ title: 'Jane Developer',
78
+ lang: 'ar',
79
+ dir: 'rtl',
80
+ });
81
+ expect(out).toContain('<title>Jane Developer</title>');
82
+ expect(out).toContain('<html lang="ar" dir="rtl">');
83
+ });
84
+
85
+ it('omits the title tag when no title is given', () => {
86
+ const out = renderResumeDocument(<Box>Hi</Box>);
87
+ expect(out).not.toContain('<title>');
88
+ });
89
+
90
+ it('includes the minimal reset only when reset is true', () => {
91
+ const withReset = renderResumeDocument(<Box>Hi</Box>, { reset: true });
92
+ expect(withReset).toContain('box-sizing:border-box');
93
+ const without = renderResumeDocument(<Box>Hi</Box>);
94
+ expect(without).not.toContain('box-sizing:border-box');
95
+ });
96
+
97
+ it('injects extra head HTML and a body class', () => {
98
+ const out = renderResumeDocument(<Box>Hi</Box>, {
99
+ head: '<meta name="robots" content="noindex">',
100
+ bodyClass: 'theme-foo',
101
+ });
102
+ expect(out).toContain('<meta name="robots" content="noindex">');
103
+ expect(out).toContain('<body class="theme-foo">');
104
+ });
105
+
106
+ it('places head before, and headAfterStyles after, the styled-components tags', () => {
107
+ const out = renderResumeDocument(<Box>Hi</Box>, {
108
+ head: '<style id="before">:root{--x:1}</style>',
109
+ headAfterStyles: '<style id="after">@page{margin:1cm}</style>',
110
+ });
111
+ const before = out.indexOf('id="before"');
112
+ const styled = out.indexOf('data-styled');
113
+ const after = out.indexOf('id="after"');
114
+ expect(before).toBeGreaterThan(-1);
115
+ expect(after).toBeGreaterThan(-1);
116
+ // cascade order: head -> styled-components -> headAfterStyles
117
+ expect(before).toBeLessThan(styled);
118
+ expect(styled).toBeLessThan(after);
119
+ });
120
+
121
+ it('omits headAfterStyles when not provided', () => {
122
+ const out = renderResumeDocument(<Box>Hi</Box>);
123
+ expect(out).not.toContain('id="after"');
124
+ });
125
+
126
+ it('always seals the sheet — styles do not leak across calls', () => {
127
+ // Two different styled components rendered in separate calls. If the sheet
128
+ // were not sealed, the second document would carry the first's rules.
129
+ const A = styled.div`
130
+ color: rgb(10, 20, 30);
131
+ `;
132
+ const B = styled.div`
133
+ color: rgb(40, 50, 60);
134
+ `;
135
+ const first = renderResumeDocument(<A>a</A>);
136
+ const second = renderResumeDocument(<B>b</B>);
137
+ expect(first).toContain('color:rgb(10, 20, 30)');
138
+ expect(first).not.toContain('color:rgb(40, 50, 60)');
139
+ expect(second).toContain('color:rgb(40, 50, 60)');
140
+ expect(second).not.toContain('color:rgb(10, 20, 30)');
141
+ });
142
+ });
@@ -0,0 +1,160 @@
1
+ /**
2
+ * @jsonresume/core/ssr
3
+ *
4
+ * Server-side render helpers for JSON Resume themes. Every JSX theme's
5
+ * `render(resume)` repeats the same boilerplate: spin up a styled-components
6
+ * ServerStyleSheet, collect styles while rendering, emit a full
7
+ * `<!DOCTYPE html>` document with Google Font <link>s in the <head>, and seal
8
+ * the sheet. This module factors that out so a theme's render() can be a single
9
+ * call to `renderResumeDocument(<Resume resume={resume} />, { fonts, title })`.
10
+ *
11
+ * SSR-safe: imports only react-dom/server + styled-components (both already
12
+ * core peer/deps) and touches no window/document at module load.
13
+ *
14
+ * @module @jsonresume/core/ssr
15
+ */
16
+ import { renderToStaticMarkup } from 'react-dom/server';
17
+ import { ServerStyleSheet } from 'styled-components';
18
+
19
+ /**
20
+ * Minimal, opt-in CSS reset (margin/padding zero + border-box). Themes that
21
+ * already ship their own reset leave this off (default); ATS-plain themes that
22
+ * want a clean slate pass `reset: true`.
23
+ */
24
+ const CSS_RESET =
25
+ '<style>*,*::before,*::after{box-sizing:border-box}' +
26
+ 'html,body{margin:0;padding:0}body{-webkit-font-smoothing:antialiased}</style>';
27
+
28
+ /** Stylesheet href for the @jsonresume/core design tokens (CSS custom props). */
29
+ const TOKENS_CSS_HREF = 'https://unpkg.com/@jsonresume/core/dist/tokens.css';
30
+
31
+ const FONTS_PRECONNECT =
32
+ '<link rel="preconnect" href="https://fonts.googleapis.com">' +
33
+ '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';
34
+
35
+ /** A value is a ready-made href if it already looks like a URL or <link> tag. */
36
+ function isHref(value) {
37
+ return /^(https?:)?\/\//.test(value) || value.trim().startsWith('<link');
38
+ }
39
+
40
+ /** Turn a Google font family name into the css2 `family=` query fragment. */
41
+ function familyParam(family) {
42
+ // Preserve any axis spec the caller appended (e.g. "Inter:wght@400;700"),
43
+ // only the family portion needs its spaces turned into '+'.
44
+ const [name, ...rest] = String(family).split(':');
45
+ const encodedName = name.trim().replace(/\s+/g, '+');
46
+ return rest.length ? `${encodedName}:${rest.join(':')}` : encodedName;
47
+ }
48
+
49
+ /**
50
+ * Build the Google Fonts <link> markup for the given families.
51
+ *
52
+ * Accepts either bare family names / `Family:axis@weights` specs (combined into
53
+ * a single css2 request) or fully-formed href strings / `<link>` tags (passed
54
+ * through verbatim). Returns an empty string when given nothing.
55
+ *
56
+ * @param {string[]} [families]
57
+ * @returns {string}
58
+ */
59
+ export function googleFontsLinks(families) {
60
+ if (!Array.isArray(families) || families.length === 0) return '';
61
+
62
+ const passthrough = [];
63
+ const names = [];
64
+ for (const entry of families) {
65
+ if (entry == null || entry === '') continue;
66
+ if (isHref(entry)) passthrough.push(entry);
67
+ else names.push(entry);
68
+ }
69
+
70
+ const links = passthrough.map((href) =>
71
+ href.trim().startsWith('<link')
72
+ ? href
73
+ : `<link href="${href}" rel="stylesheet">`
74
+ );
75
+
76
+ if (names.length > 0) {
77
+ const query = names.map(familyParam).join('&family=');
78
+ links.unshift(
79
+ `<link href="https://fonts.googleapis.com/css2?family=${query}&display=swap" rel="stylesheet">`
80
+ );
81
+ }
82
+
83
+ if (links.length === 0) return '';
84
+ return FONTS_PRECONNECT + links.join('');
85
+ }
86
+
87
+ /**
88
+ * Render a React element to a complete, styled HTML document string using the
89
+ * same ServerStyleSheet path themes hand-roll today. The sheet is ALWAYS sealed
90
+ * in a finally block so collected styles never leak between renders.
91
+ *
92
+ * @param {import('react').ReactElement} element Root resume element to render.
93
+ * @param {object} [options]
94
+ * @param {string[]} [options.fonts] Google font families OR hrefs.
95
+ * @param {string} [options.title] <title> text (omitted if falsy).
96
+ * @param {string} [options.lang='en'] <html lang> attribute.
97
+ * @param {string} [options.dir='ltr'] <html dir> attribute.
98
+ * @param {boolean} [options.reset=false] Include the minimal CSS reset.
99
+ * @param {string} [options.head=''] Raw HTML inserted in <head> BEFORE the
100
+ * styled-components tags (base styles a theme's components then override — e.g. a
101
+ * `:root` design-token block or a CSS reset).
102
+ * @param {string} [options.headAfterStyles=''] Raw HTML inserted in <head> AFTER the
103
+ * styled-components tags (styles that must OVERRIDE component styles — e.g. a global
104
+ * reset, body background, or `@media print`/`@page` rules). Composes with `head`:
105
+ * a theme can pass both to reproduce an inline-style block on each side.
106
+ * @param {boolean} [options.includeTokensCss=true] Link @jsonresume/core tokens.
107
+ * @param {string} [options.bodyClass] class attribute on <body>.
108
+ * @returns {string} Full `<!DOCTYPE html>...` document.
109
+ */
110
+ export function renderResumeDocument(element, options = {}) {
111
+ const {
112
+ fonts,
113
+ title,
114
+ lang = 'en',
115
+ dir = 'ltr',
116
+ reset = false,
117
+ head = '',
118
+ headAfterStyles = '',
119
+ includeTokensCss = true,
120
+ bodyClass,
121
+ } = options;
122
+
123
+ const sheet = new ServerStyleSheet();
124
+ let html;
125
+ let styleTags;
126
+ try {
127
+ html = renderToStaticMarkup(sheet.collectStyles(element));
128
+ styleTags = sheet.getStyleTags();
129
+ } finally {
130
+ sheet.seal();
131
+ }
132
+
133
+ const fontLinks = googleFontsLinks(fonts);
134
+ const tokensLink = includeTokensCss
135
+ ? `<link rel="stylesheet" href="${TOKENS_CSS_HREF}">`
136
+ : '';
137
+ const resetTag = reset ? CSS_RESET : '';
138
+ const titleTag = title ? `<title>${title}</title>` : '';
139
+ const bodyAttr = bodyClass ? ` class="${bodyClass}"` : '';
140
+
141
+ return (
142
+ '<!DOCTYPE html>' +
143
+ `<html lang="${lang}" dir="${dir}">` +
144
+ '<head>' +
145
+ '<meta charset="utf-8">' +
146
+ '<meta name="viewport" content="width=device-width, initial-scale=1">' +
147
+ fontLinks +
148
+ tokensLink +
149
+ resetTag +
150
+ head +
151
+ styleTags +
152
+ headAfterStyles +
153
+ titleTag +
154
+ '</head>' +
155
+ `<body${bodyAttr}>${html}</body>` +
156
+ '</html>'
157
+ );
158
+ }
159
+
160
+ export default { renderResumeDocument, googleFontsLinks };