@jsonresume/core 0.2.0 → 0.3.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.
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jsonresume/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
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.
|
|
39
|
-
"@jsonresume/utils": "0.2.
|
|
39
|
+
"@jsonresume/types": "0.2.1",
|
|
40
|
+
"@jsonresume/utils": "0.2.1"
|
|
40
41
|
},
|
|
41
42
|
"keywords": [
|
|
42
43
|
"resume",
|
|
@@ -0,0 +1,122 @@
|
|
|
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('always seals the sheet — styles do not leak across calls', () => {
|
|
107
|
+
// Two different styled components rendered in separate calls. If the sheet
|
|
108
|
+
// were not sealed, the second document would carry the first's rules.
|
|
109
|
+
const A = styled.div`
|
|
110
|
+
color: rgb(10, 20, 30);
|
|
111
|
+
`;
|
|
112
|
+
const B = styled.div`
|
|
113
|
+
color: rgb(40, 50, 60);
|
|
114
|
+
`;
|
|
115
|
+
const first = renderResumeDocument(<A>a</A>);
|
|
116
|
+
const second = renderResumeDocument(<B>b</B>);
|
|
117
|
+
expect(first).toContain('color:rgb(10, 20, 30)');
|
|
118
|
+
expect(first).not.toContain('color:rgb(40, 50, 60)');
|
|
119
|
+
expect(second).toContain('color:rgb(40, 50, 60)');
|
|
120
|
+
expect(second).not.toContain('color:rgb(10, 20, 30)');
|
|
121
|
+
});
|
|
122
|
+
});
|
package/src/ssr/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
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=''] Extra raw HTML for the <head>.
|
|
100
|
+
* @param {boolean} [options.includeTokensCss=true] Link @jsonresume/core tokens.
|
|
101
|
+
* @param {string} [options.bodyClass] class attribute on <body>.
|
|
102
|
+
* @returns {string} Full `<!DOCTYPE html>...` document.
|
|
103
|
+
*/
|
|
104
|
+
export function renderResumeDocument(element, options = {}) {
|
|
105
|
+
const {
|
|
106
|
+
fonts,
|
|
107
|
+
title,
|
|
108
|
+
lang = 'en',
|
|
109
|
+
dir = 'ltr',
|
|
110
|
+
reset = false,
|
|
111
|
+
head = '',
|
|
112
|
+
includeTokensCss = true,
|
|
113
|
+
bodyClass,
|
|
114
|
+
} = options;
|
|
115
|
+
|
|
116
|
+
const sheet = new ServerStyleSheet();
|
|
117
|
+
let html;
|
|
118
|
+
let styleTags;
|
|
119
|
+
try {
|
|
120
|
+
html = renderToStaticMarkup(sheet.collectStyles(element));
|
|
121
|
+
styleTags = sheet.getStyleTags();
|
|
122
|
+
} finally {
|
|
123
|
+
sheet.seal();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const fontLinks = googleFontsLinks(fonts);
|
|
127
|
+
const tokensLink = includeTokensCss
|
|
128
|
+
? `<link rel="stylesheet" href="${TOKENS_CSS_HREF}">`
|
|
129
|
+
: '';
|
|
130
|
+
const resetTag = reset ? CSS_RESET : '';
|
|
131
|
+
const titleTag = title ? `<title>${title}</title>` : '';
|
|
132
|
+
const bodyAttr = bodyClass ? ` class="${bodyClass}"` : '';
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
'<!DOCTYPE html>' +
|
|
136
|
+
`<html lang="${lang}" dir="${dir}">` +
|
|
137
|
+
'<head>' +
|
|
138
|
+
'<meta charset="utf-8">' +
|
|
139
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">' +
|
|
140
|
+
fontLinks +
|
|
141
|
+
tokensLink +
|
|
142
|
+
resetTag +
|
|
143
|
+
head +
|
|
144
|
+
styleTags +
|
|
145
|
+
titleTag +
|
|
146
|
+
'</head>' +
|
|
147
|
+
`<body${bodyAttr}>${html}</body>` +
|
|
148
|
+
'</html>'
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export default { renderResumeDocument, googleFontsLinks };
|