From 582d629057ffdd8a8f679472f65dcf360858a775 Mon Sep 17 00:00:00 2001 From: Reorx Date: Sat, 4 Feb 2023 22:38:36 +0800 Subject: [PATCH] handle date format in template --- src/lib/date.js | 5 ++++ src/main.js | 8 +++++- src/templates/reorx/index.ejs | 51 ++++++++++++++++++++++------------- 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 src/lib/date.js diff --git a/src/lib/date.js b/src/lib/date.js new file mode 100644 index 0000000..d89fc5e --- /dev/null +++ b/src/lib/date.js @@ -0,0 +1,5 @@ +import dayjs from 'dayjs'; + +export function reformatDate(dateStr, format) { + return dayjs(dateStr).format(format) +} diff --git a/src/main.js b/src/main.js index db6c721..bb4813d 100644 --- a/src/main.js +++ b/src/main.js @@ -1,7 +1,13 @@ import * as exampleData from '../data/rxresume-converted.json'; +import { reformatDate } from './lib/date'; import { applyThemeTo } from './themer'; const elCV = document.querySelector('.cv-container') -applyThemeTo('default', exampleData, elCV) +applyThemeTo('default', { + cv: exampleData, + fn: { + reformatDate, + } +}, elCV) diff --git a/src/templates/reorx/index.ejs b/src/templates/reorx/index.ejs index 81279bf..26ced3c 100644 --- a/src/templates/reorx/index.ejs +++ b/src/templates/reorx/index.ejs @@ -1,35 +1,48 @@ -

<%= basics.name %>

-

<%= basics.summary %>

+

<%= cv.basics.name %>

+

<%= cv.basics.summary %>

- <%= basics.email %> | <%= basics.phone %> + <%= cv.basics.email %> | <%= cv.basics.phone %>
+<% +function dateRange(item, preserveDay) { + let {startDate, endDate} = item + if (!startDate && !endDate) return '' + if (!preserveDay) { + const format = 'YYYY-MM' + startDate = fn.reformatDate(startDate, format) + endDate = fn.reformatDate(endDate, format) + } + return `

${startDate}~${endDate}

` +} +%> + -<% if (locals.education) { %> +<% if (cv.education) { %>

Educations

- <% for (const item of education) { %> + <% for (const item of cv.education) { %>

<%= item.institution %>

<%= item.url %>

<%= item.area %>

<%= item.score %>

-

<%= item.startDate %> - <%= item.endDate %>

+ <%- dateRange(item) %>
<% } %>
<% } %> -<% if (locals.work) { %> +<% if (cv.work) { %>

Work

- <% for (const item of work) { %> + <% for (const item of cv.work) { %>

<%= item.name %>

<%= item.url %>

-

<%= item.startDate %> - <%= item.endDate %>

+ <%- dateRange(item) %>

<%= item.position %>

<%= item.summary %>
@@ -43,15 +56,15 @@ <% } %> -<% if (locals.projects) { %> +<% if (cv.projects) { %>

Projects

- <% for (const item of projects) { %> + <% for (const item of cv.projects) { %>

<%= item.name %>

<%= item.url %>

<%= item.description %>

-

<%= item.startDate %> - <%= item.endDate %>

+ <%- dateRange(item) %>
<% for (const hl of item.highlights) { %>
<%= hl %>
@@ -68,15 +81,15 @@ <% } %> -<% if (locals.sideProjects) { %> +<% if (cv.sideProjects) { %>

Side-projects

- <% for (const item of sideProjects) { %> + <% for (const item of cv.sideProjects) { %>

<%= item.name %>

<%= item.url %>

<%= item.description %>

-

<%= item.startDate %> - <%= item.endDate %>

+ <%- dateRange(item) %>
<% for (const kw of item.keywords) { %> <%= kw %> @@ -88,10 +101,10 @@ <% } %> -<% if (locals.skills) { %> +<% if (cv.skills) { %>

Skills

- <% for (const item of skills) { %> + <% for (const item of cv.skills) { %>

<%= item.name %>

<%= item.level %>
@@ -106,10 +119,10 @@ <% } %> -<% if (locals.languages) { %> +<% if (cv.languages) { %>

Languages

- <% for (const item of languages) { %> + <% for (const item of cv.languages) { %>

<%= item.language %>

<%= item.fluency %>