diff --git a/src/components/Byline/Byline.mdx b/src/components/Byline/Byline.mdx index 779d9aae..c47a7459 100644 --- a/src/components/Byline/Byline.mdx +++ b/src/components/Byline/Byline.mdx @@ -6,7 +6,7 @@ import * as BylineStories from './Byline.stories.svelte'; # Byline -Easily add a byline and dateline. +Easily add a byline and dateline to your story. ```svelte -{#snippet template(args: ComponentProps)} - -{/snippet} - + + + `https://www.reuters.com/graphics/`} + publishTime="2021-09-12T00:00:00Z" + updateTime="2021-09-12T13:57:00Z" + > + {#snippet customPublished()} + PUBLISHED on some custom date and time + {/snippet} + {#snippet customUpdated()} + Updated every 5 minutes + {/snippet} + + diff --git a/src/components/Byline/Byline.svelte b/src/components/Byline/Byline.svelte index 8f67a4e7..fe58bba1 100644 --- a/src/components/Byline/Byline.svelte +++ b/src/components/Byline/Byline.svelte @@ -4,63 +4,86 @@ import Block from '../Block/Block.svelte'; import slugify from 'slugify'; import { apdate } from 'journalize'; + import type { Snippet } from 'svelte'; + + interface Props { + /** + * Array of author names, which will be slugified to create links to Reuters author pages + */ + authors: string[]; + /** + * Publish time as a datetime string. + */ + publishTime: string; + /** + * Update time as a datetime string. + * @type {string} + */ + updateTime: string; + /** + * Alignment of the byline. + * @type {string} + */ + align?: 'left' | 'center'; + /** + * Add an id to to target with custom CSS. + * @type {string} + */ + id?: string; + /** + * Add extra classes to target with custom CSS. + * @type {string} + */ + cls?: string; + /** + * Custom function that returns an author page URL. + */ + getAuthorPage?: (author: string) => string; + /** + * Optional snippet for a custom published dateline. + */ + // Specify that this prop should have the type of a Svelte snippet, i.e. basic html + customPublished?: Snippet | null; + /** + * Optional snippet for a custom updated dateline. + */ + customUpdated?: Snippet | null; + } + + let { + authors = [], + publishTime = '', + updateTime = '', + align = 'left', + id = '', + cls = '', + getAuthorPage = (author: string): string => { + const authorSlug = slugify(author.trim(), { lower: true }); + return `https://www.reuters.com/authors/${authorSlug}/`; + }, + customPublished = null, + customUpdated = null, + }: Props = $props(); + + let alignmentClass = $derived(align === 'left' ? 'text-left' : 'text-center'); /** - * Array of author names, which will be slugified to create links to Reuters author pages + /* Date validation and formatter functions */ - export let authors: string[] = []; - /** - * Publish time as a datetime string. - * @type {string} - */ - export let publishTime: string = ''; - /** - * Update time as a datetime string. - * @type {string} - */ - export let updateTime: string = ''; - /** - * Alignment of the byline. - * @type {string} - */ - export let align: 'left' | 'center' = 'left'; - /** - * Add an id to to target with custom CSS. - * @type {string} - */ - export let id: string = ''; - /** - * Add extra classes to target with custom CSS. - * @type {string} - */ - let cls: string = ''; - export { cls as class }; - - /** - * Custom function that returns an author page URL. - * @param author - */ - export let getAuthorPage = (author: string): string => { - const authorSlug = slugify(author.trim(), { lower: true }); - return `https://www.reuters.com/authors/${authorSlug}/`; - }; - - $: alignmentClass = align === 'left' ? 'text-left' : 'text-center'; - - const isValidDate = (datetime) => { + const isValidDate = (datetime: string) => { if (!datetime) return false; if (!Date.parse(datetime)) return false; return true; }; - const formatTime = (datetime) => + const formatTime = (datetime: string) => new Date(datetime).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', timeZoneName: 'short', }); - const areSameDay = (first, second) => + const areSameDay = (first: Date, second: Date) => first.getFullYear() === second.getFullYear() && first.getMonth() === second.getMonth() && first.getDate() === second.getDate(); @@ -69,41 +92,38 @@