@jsonresume/core 0.1.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/LICENSE +21 -0
- package/package.json +22 -18
- package/src/__tests__/components/contactInfo.test.jsx +96 -0
- package/src/__tests__/components/dateRange.test.jsx +119 -0
- package/src/__tests__/components/link.test.jsx +115 -0
- package/src/__tests__/components/primitives.test.jsx +199 -0
- package/src/__tests__/components/profile.test.jsx +113 -0
- package/src/__tests__/components/renderHtml.jsx +24 -0
- package/src/__tests__/ssr/renderResumeDocument.test.jsx +122 -0
- package/src/dates/RelativeDate.jsx +4 -72
- package/src/helpers/calculations.js +40 -422
- package/src/helpers/counts.js +16 -0
- package/src/helpers/education.js +12 -0
- package/src/helpers/experience.js +16 -0
- package/src/helpers/index.js +11 -0
- package/src/helpers/keyMetrics.js +8 -0
- package/src/helpers/workHistory.js +13 -0
- package/src/index.js +8 -3
- package/src/primitives/DateRange.jsx +5 -107
- package/src/ssr/index.js +152 -0
- package/src/styled.d.ts +46 -0
- package/src/utils/security.js +13 -154
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 };
|
package/src/styled.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import 'styled-components';
|
|
2
|
+
|
|
3
|
+
declare module 'styled-components' {
|
|
4
|
+
export interface DefaultTheme {
|
|
5
|
+
colors?: {
|
|
6
|
+
primary?: string;
|
|
7
|
+
secondary?: string;
|
|
8
|
+
tertiary?: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
link?: string;
|
|
11
|
+
linkVisited?: string;
|
|
12
|
+
accent?: string;
|
|
13
|
+
accentLight?: string;
|
|
14
|
+
accentMuted?: string;
|
|
15
|
+
accentBorder?: string;
|
|
16
|
+
muted?: string;
|
|
17
|
+
border?: string;
|
|
18
|
+
borderLight?: string;
|
|
19
|
+
};
|
|
20
|
+
spacing?: {
|
|
21
|
+
section?: string;
|
|
22
|
+
item?: string;
|
|
23
|
+
small?: string;
|
|
24
|
+
tight?: string;
|
|
25
|
+
};
|
|
26
|
+
typography?: {
|
|
27
|
+
heading?: string;
|
|
28
|
+
body?: string;
|
|
29
|
+
small?: string;
|
|
30
|
+
lineHeight?: string;
|
|
31
|
+
weightMedium?: string;
|
|
32
|
+
sizes?: {
|
|
33
|
+
small?: string;
|
|
34
|
+
};
|
|
35
|
+
weights?: {
|
|
36
|
+
medium?: string;
|
|
37
|
+
semibold?: string;
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
radius?: {
|
|
41
|
+
sm?: string;
|
|
42
|
+
md?: string;
|
|
43
|
+
full?: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/utils/security.js
CHANGED
|
@@ -1,155 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Security utilities for @
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
* @example
|
|
16
|
-
* safeUrl('https://example.com') // 'https://example.com'
|
|
17
|
-
* safeUrl('javascript:alert(1)') // null
|
|
18
|
-
* safeUrl('mailto:user@example.com') // 'mailto:user@example.com'
|
|
19
|
-
*/
|
|
20
|
-
export function safeUrl(url) {
|
|
21
|
-
if (!url || typeof url !== 'string') {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// Trim whitespace
|
|
26
|
-
const trimmed = url.trim();
|
|
27
|
-
|
|
28
|
-
// Check for dangerous protocols
|
|
29
|
-
const dangerousProtocols = /^(javascript|data|vbscript|file|about):/i;
|
|
30
|
-
if (dangerousProtocols.test(trimmed)) {
|
|
31
|
-
console.warn(`[Security] Blocked dangerous URL: ${trimmed.slice(0, 50)}`);
|
|
32
|
-
return null;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Allow safe protocols
|
|
36
|
-
const safeProtocols = /^(https?|mailto|tel|sms|ftp):/i;
|
|
37
|
-
if (safeProtocols.test(trimmed)) {
|
|
38
|
-
return trimmed;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Allow relative URLs (starting with / or .)
|
|
42
|
-
if (trimmed.startsWith('/') || trimmed.startsWith('.')) {
|
|
43
|
-
return trimmed;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Allow URLs without protocol (assume https)
|
|
47
|
-
if (/^www\./i.test(trimmed)) {
|
|
48
|
-
return `https://${trimmed}`;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// For other cases, check if it looks like a valid domain
|
|
52
|
-
// Allow alphanumeric, hyphens, dots, and common TLDs
|
|
53
|
-
if (/^[a-z0-9][a-z0-9.-]+\.[a-z]{2,}$/i.test(trimmed)) {
|
|
54
|
-
return `https://${trimmed}`;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// If we can't determine safety, return the original
|
|
58
|
-
// (Let the browser handle it, but log a warning)
|
|
59
|
-
console.warn(`[Security] Uncertain URL safety: ${trimmed.slice(0, 50)}`);
|
|
60
|
-
return trimmed;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Returns proper rel attribute for external links
|
|
65
|
-
* Adds security attributes for links opening in new windows
|
|
66
|
-
*
|
|
67
|
-
* @param {string} url - The URL to check
|
|
68
|
-
* @param {boolean} openInNewTab - Whether link opens in new tab
|
|
69
|
-
* @returns {string} - rel attribute value
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* getLinkRel('https://example.com', true) // 'noopener noreferrer'
|
|
73
|
-
* getLinkRel('mailto:user@example.com', false) // ''
|
|
74
|
-
*/
|
|
75
|
-
export function getLinkRel(url, openInNewTab = false) {
|
|
76
|
-
if (!url || typeof url !== 'string') {
|
|
77
|
-
return '';
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Only add security attributes for http(s) links opening in new tabs
|
|
81
|
-
if (openInNewTab && /^https?:/i.test(url)) {
|
|
82
|
-
return 'noopener noreferrer';
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return '';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Sanitizes HTML to prevent XSS
|
|
90
|
-
* Simple implementation that escapes dangerous characters
|
|
91
|
-
* For more complex needs, use DOMPurify
|
|
92
|
-
*
|
|
93
|
-
* @param {string} html - HTML string to sanitize
|
|
94
|
-
* @returns {string} - Sanitized HTML
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* sanitizeHtml('<script>alert(1)</script>') // '<script>alert(1)</script>'
|
|
98
|
-
*/
|
|
99
|
-
export function sanitizeHtml(html) {
|
|
100
|
-
if (!html || typeof html !== 'string') {
|
|
101
|
-
return '';
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return html
|
|
105
|
-
.replace(/&/g, '&')
|
|
106
|
-
.replace(/</g, '<')
|
|
107
|
-
.replace(/>/g, '>')
|
|
108
|
-
.replace(/"/g, '"')
|
|
109
|
-
.replace(/'/g, ''');
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Checks if a URL is external (different origin)
|
|
114
|
-
*
|
|
115
|
-
* @param {string} url - URL to check
|
|
116
|
-
* @param {string} currentOrigin - Current site origin (default: window.location.origin if available)
|
|
117
|
-
* @returns {boolean} - True if URL is external
|
|
118
|
-
*
|
|
119
|
-
* @example
|
|
120
|
-
* isExternalUrl('https://example.com') // true (if on different domain)
|
|
121
|
-
* isExternalUrl('/about') // false
|
|
122
|
-
*/
|
|
123
|
-
export function isExternalUrl(url, currentOrigin = null) {
|
|
124
|
-
if (!url || typeof url !== 'string') {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// Relative URLs are not external
|
|
129
|
-
if (url.startsWith('/') || url.startsWith('.') || url.startsWith('#')) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// mailto:, tel:, etc. are not external in the HTTP sense
|
|
134
|
-
if (/^(mailto|tel|sms):/i.test(url)) {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// If no origin provided and we're in a browser, use window.location
|
|
139
|
-
if (!currentOrigin && typeof window !== 'undefined') {
|
|
140
|
-
currentOrigin = window.location.origin;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// If still no origin, we can't determine (assume external for safety)
|
|
144
|
-
if (!currentOrigin) {
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
const urlObj = new URL(url, currentOrigin);
|
|
150
|
-
return urlObj.origin !== currentOrigin;
|
|
151
|
-
} catch (e) {
|
|
152
|
-
// Invalid URL, treat as external for safety
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
2
|
+
* Security utilities for @jsonresume/core
|
|
3
|
+
*
|
|
4
|
+
* The implementations now live in @jsonresume/utils (framework-free). This
|
|
5
|
+
* module re-exports them so existing imports
|
|
6
|
+
* (`import { safeUrl } from '@jsonresume/core'` and the internal
|
|
7
|
+
* `../utils/security.js` imports in Link/ContactInfo) keep working unchanged.
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
safeUrl,
|
|
11
|
+
getLinkRel,
|
|
12
|
+
sanitizeHtml,
|
|
13
|
+
isExternalUrl,
|
|
14
|
+
} from '@jsonresume/utils/url';
|