@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
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { renderHtml } from './renderHtml.jsx';
|
|
4
|
+
import { Avatar, ProfileCard, SocialLinks } from '../../index.js';
|
|
5
|
+
|
|
6
|
+
describe('Avatar', () => {
|
|
7
|
+
it('renders an <img> when src is provided', () => {
|
|
8
|
+
const { html } = renderHtml(
|
|
9
|
+
<Avatar src="https://img.example.com/a.png" alt="Jane Doe" />
|
|
10
|
+
);
|
|
11
|
+
expect(html).toContain('<img');
|
|
12
|
+
expect(html).toContain('src="https://img.example.com/a.png"');
|
|
13
|
+
expect(html).toContain('alt="Jane Doe"');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('renders a placeholder with the first initial when no src/fallback', () => {
|
|
17
|
+
const { html } = renderHtml(<Avatar alt="Jane" />);
|
|
18
|
+
expect(html).not.toContain('<img');
|
|
19
|
+
expect(html).toContain('J');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('renders "?" placeholder when there is no src, fallback, or alt', () => {
|
|
23
|
+
const { html } = renderHtml(<Avatar />);
|
|
24
|
+
expect(html).not.toContain('<img');
|
|
25
|
+
expect(html).toContain('?');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('uses the fallback image when src is absent but fallback is set', () => {
|
|
29
|
+
const { html } = renderHtml(
|
|
30
|
+
<Avatar fallback="https://img.example.com/f.png" alt="x" />
|
|
31
|
+
);
|
|
32
|
+
expect(html).toContain('<img');
|
|
33
|
+
expect(html).toContain('https://img.example.com/f.png');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('uppercases the placeholder initial', () => {
|
|
37
|
+
const { html } = renderHtml(<Avatar alt="zara" />);
|
|
38
|
+
expect(html).toContain('Z');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('passes className through to the rendered element', () => {
|
|
42
|
+
const { html } = renderHtml(<Avatar src="x.png" className="rounded" />);
|
|
43
|
+
expect(html).toContain('rounded');
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('ProfileCard', () => {
|
|
48
|
+
it('renders name, title and summary when provided', () => {
|
|
49
|
+
const { html } = renderHtml(
|
|
50
|
+
<ProfileCard
|
|
51
|
+
name="Jane Doe"
|
|
52
|
+
title="Engineer"
|
|
53
|
+
summary="Builds resilient systems"
|
|
54
|
+
/>
|
|
55
|
+
);
|
|
56
|
+
expect(html).toContain('Jane Doe');
|
|
57
|
+
expect(html).toContain('Engineer');
|
|
58
|
+
expect(html).toContain('Builds resilient systems');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('renders without any fields (no crash, empty info)', () => {
|
|
62
|
+
const { html } = renderHtml(<ProfileCard />);
|
|
63
|
+
expect(html).toContain('<div');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('renders a provided avatar node and children', () => {
|
|
67
|
+
const { html } = renderHtml(
|
|
68
|
+
<ProfileCard name="Jane" avatar={<span className="av">AV</span>}>
|
|
69
|
+
<span className="child">extra</span>
|
|
70
|
+
</ProfileCard>
|
|
71
|
+
);
|
|
72
|
+
expect(html).toContain('AV');
|
|
73
|
+
expect(html).toContain('extra');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('SocialLinks', () => {
|
|
78
|
+
it('renders an anchor per link with target/rel', () => {
|
|
79
|
+
const { html } = renderHtml(
|
|
80
|
+
<SocialLinks
|
|
81
|
+
links={[
|
|
82
|
+
{
|
|
83
|
+
network: 'GitHub',
|
|
84
|
+
username: 'jane',
|
|
85
|
+
url: 'https://github.com/jane',
|
|
86
|
+
},
|
|
87
|
+
{ network: 'X', username: 'jane', url: 'https://x.com/jane' },
|
|
88
|
+
]}
|
|
89
|
+
/>
|
|
90
|
+
);
|
|
91
|
+
const anchors = (html.match(/<a /g) || []).length;
|
|
92
|
+
expect(anchors).toBe(2);
|
|
93
|
+
expect(html).toContain('rel="noopener noreferrer"');
|
|
94
|
+
expect(html).toContain('https://github.com/jane');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('renders nothing meaningful for an empty links list', () => {
|
|
98
|
+
const { html } = renderHtml(<SocialLinks links={[]} />);
|
|
99
|
+
expect(html).not.toContain('<a ');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('does not crash when links prop is omitted (defaults to [])', () => {
|
|
103
|
+
const { html } = renderHtml(<SocialLinks />);
|
|
104
|
+
expect(html).toContain('<div');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('falls back to network when username is missing', () => {
|
|
108
|
+
const { html } = renderHtml(
|
|
109
|
+
<SocialLinks links={[{ network: 'Dribbble', url: 'https://x' }]} />
|
|
110
|
+
);
|
|
111
|
+
expect(html).toContain('Dribbble');
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
3
|
+
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Render a @jsonresume/core component to static HTML using the same
|
|
7
|
+
* ServerStyleSheet path themes use for SSR. Returns both the markup and the
|
|
8
|
+
* collected style tags so tests can assert on output without a DOM or DB.
|
|
9
|
+
*
|
|
10
|
+
* @param {React.ReactElement} element
|
|
11
|
+
* @returns {{ html: string, css: string }}
|
|
12
|
+
*/
|
|
13
|
+
export function renderHtml(element) {
|
|
14
|
+
const sheet = new ServerStyleSheet();
|
|
15
|
+
try {
|
|
16
|
+
const html = renderToStaticMarkup(
|
|
17
|
+
<StyleSheetManager sheet={sheet.instance}>{element}</StyleSheetManager>
|
|
18
|
+
);
|
|
19
|
+
const css = sheet.getStyleTags();
|
|
20
|
+
return { html, css };
|
|
21
|
+
} finally {
|
|
22
|
+
sheet.seal();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -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
|
+
});
|
|
@@ -2,8 +2,12 @@ import React from 'react';
|
|
|
2
2
|
/**
|
|
3
3
|
* RelativeDate
|
|
4
4
|
* Display relative date (e.g., "2 years ago", "3 months")
|
|
5
|
+
*
|
|
6
|
+
* The pure getRelativeTime / getDuration helpers now live in @jsonresume/utils
|
|
7
|
+
* (framework-free); this component imports them back.
|
|
5
8
|
*/
|
|
6
9
|
import styled from 'styled-components';
|
|
10
|
+
import { getRelativeTime, getDuration } from '@jsonresume/utils/dates';
|
|
7
11
|
|
|
8
12
|
const Container = styled.time`
|
|
9
13
|
font-size: ${(props) => props.$size || 'var(--resume-size-small)'};
|
|
@@ -11,78 +15,6 @@ const Container = styled.time`
|
|
|
11
15
|
white-space: nowrap;
|
|
12
16
|
`;
|
|
13
17
|
|
|
14
|
-
/**
|
|
15
|
-
* Calculate relative time from a date
|
|
16
|
-
* @param {string|Date} date - Date to calculate from
|
|
17
|
-
* @param {boolean} ago - Include "ago" suffix
|
|
18
|
-
* @returns {string} Relative time string
|
|
19
|
-
*/
|
|
20
|
-
function getRelativeTime(date, ago = true) {
|
|
21
|
-
const now = new Date();
|
|
22
|
-
const past = new Date(date);
|
|
23
|
-
const diffMs = now - past;
|
|
24
|
-
const diffSec = Math.floor(diffMs / 1000);
|
|
25
|
-
const diffMin = Math.floor(diffSec / 60);
|
|
26
|
-
const diffHour = Math.floor(diffMin / 60);
|
|
27
|
-
const diffDay = Math.floor(diffHour / 24);
|
|
28
|
-
const diffWeek = Math.floor(diffDay / 7);
|
|
29
|
-
const diffMonth = Math.floor(diffDay / 30);
|
|
30
|
-
const diffYear = Math.floor(diffDay / 365);
|
|
31
|
-
|
|
32
|
-
const suffix = ago ? ' ago' : '';
|
|
33
|
-
|
|
34
|
-
if (diffYear > 0) {
|
|
35
|
-
return `${diffYear} year${diffYear !== 1 ? 's' : ''}${suffix}`;
|
|
36
|
-
}
|
|
37
|
-
if (diffMonth > 0) {
|
|
38
|
-
return `${diffMonth} month${diffMonth !== 1 ? 's' : ''}${suffix}`;
|
|
39
|
-
}
|
|
40
|
-
if (diffWeek > 0) {
|
|
41
|
-
return `${diffWeek} week${diffWeek !== 1 ? 's' : ''}${suffix}`;
|
|
42
|
-
}
|
|
43
|
-
if (diffDay > 0) {
|
|
44
|
-
return `${diffDay} day${diffDay !== 1 ? 's' : ''}${suffix}`;
|
|
45
|
-
}
|
|
46
|
-
if (diffHour > 0) {
|
|
47
|
-
return `${diffHour} hour${diffHour !== 1 ? 's' : ''}${suffix}`;
|
|
48
|
-
}
|
|
49
|
-
if (diffMin > 0) {
|
|
50
|
-
return `${diffMin} minute${diffMin !== 1 ? 's' : ''}${suffix}`;
|
|
51
|
-
}
|
|
52
|
-
return 'just now';
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Calculate duration between two dates
|
|
57
|
-
* @param {string|Date} startDate - Start date
|
|
58
|
-
* @param {string|Date} endDate - End date (defaults to now)
|
|
59
|
-
* @returns {string} Duration string
|
|
60
|
-
*/
|
|
61
|
-
function getDuration(startDate, endDate = new Date()) {
|
|
62
|
-
const start = new Date(startDate);
|
|
63
|
-
const end = new Date(endDate);
|
|
64
|
-
const diffMs = end - start;
|
|
65
|
-
const diffDay = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
|
66
|
-
const diffMonth = Math.floor(diffDay / 30);
|
|
67
|
-
const diffYear = Math.floor(diffDay / 365);
|
|
68
|
-
|
|
69
|
-
const years = diffYear;
|
|
70
|
-
const months = diffMonth % 12;
|
|
71
|
-
|
|
72
|
-
if (years > 0 && months > 0) {
|
|
73
|
-
return `${years} year${years !== 1 ? 's' : ''}, ${months} month${
|
|
74
|
-
months !== 1 ? 's' : ''
|
|
75
|
-
}`;
|
|
76
|
-
}
|
|
77
|
-
if (years > 0) {
|
|
78
|
-
return `${years} year${years !== 1 ? 's' : ''}`;
|
|
79
|
-
}
|
|
80
|
-
if (months > 0) {
|
|
81
|
-
return `${months} month${months !== 1 ? 's' : ''}`;
|
|
82
|
-
}
|
|
83
|
-
return `${diffDay} day${diffDay !== 1 ? 's' : ''}`;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
18
|
/**
|
|
87
19
|
* @param {Object} props
|
|
88
20
|
* @param {string|Date} props.date - Date to display relatively
|