Skip to content

Commit

Permalink
fix see more
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed Mar 1, 2024
1 parent feb4466 commit e5518b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
11 changes: 11 additions & 0 deletions app/components/Article/Contents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ const Contents = ({pageid, html, glossary}: {pageid: PageId; html: string; gloss

updateTextNodes(el, insertGlossary(pageid, glossary))

const toggleClass = (e: Event) => {
e.preventDefault()
const target = e.target as HTMLElement
target.classList.toggle('visible')
}
Array.from(document.getElementsByClassName('see-more')).forEach((e) => {
const elem = e.cloneNode(true)
elem.addEventListener('click', toggleClass)
e.replaceWith(elem)
})

// In theory this could be extended to all links
el.querySelectorAll('.footnote-ref > a').forEach((e) => {
const footnote = footnoteHTML(el, e as HTMLAnchorElement)
Expand Down
2 changes: 1 addition & 1 deletion app/server-utils/parsing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const convertToHtmlAndWrapInDetails = (markdown: string): string => {
// On the other hand, if the whole text isn't rendered as one, footnotes will break.
// To get round this, replace the [See more...] button with a magic string, render the
// markdown, then mangle the resulting HTML to add an appropriate button link
markdown = markdown.replaceAll(/\s*\[[Ss]ee more\W*?\]\s*/g, seeMoreToken)
markdown = markdown.replaceAll(/\[[Ss]ee more\W*?\]/g, seeMoreToken)
markdown = md.render(markdown)
markdown = wrap(markdown.split(seeMoreToken))

Expand Down
17 changes: 13 additions & 4 deletions app/server-utils/stampy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export type AnswersRow = CodaRowCommon & {
Banners: '' | Entity[]
'Rich Text': string
Subtitle?: string
Icon?: string
Icon?: Entity | string | Entity[]
Parents?: Entity[]
Order?: number
}
Expand Down Expand Up @@ -250,7 +250,7 @@ const renderText = (pageid: PageId, markdown: string | null): string | null => {
if (!markdown) return null

markdown = extractText(markdown)
markdown = urlToIframe(markdown)
markdown = urlToIframe(markdown || '')

let html = convertToHtmlAndWrapInDetails(markdown)
html = uniqueFootnotes(html, pageid)
Expand All @@ -260,8 +260,17 @@ const renderText = (pageid: PageId, markdown: string | null): string | null => {
return html
}

// Icons can be returned as strings or objects
const extractIcon = (val?: string | string[] | Entity | Entity[]): string | undefined => {
if (!val) return val
const item = head(val)
if (typeof item === 'string') return extractText(val as string)
if (item) return (item as Entity).url
return undefined
}

// Sometimes string fields are returned as lists. This can happen when there are duplicate entries in Coda
const head = (item: string | string[]) => {
const head = (item: any | any[]) => {
if (Array.isArray(item)) return item[0]
return item
}
Expand Down Expand Up @@ -291,7 +300,7 @@ const convertToQuestion = ({name, values, updatedAt} = {} as AnswersRow): Questi
status: values['Status']?.name as QuestionStatus,
alternatePhrasings: extractText(values['Alternate Phrasings']),
subtitle: extractText(values.Subtitle),
icon: extractText(values.Icon),
icon: extractIcon(values.Icon),
parents: !values.Parents ? [] : values.Parents?.map(({name}) => name),
updatedAt: updatedAt || values['Doc Last Edited'],
order: values.Order || 0,
Expand Down

0 comments on commit e5518b7

Please sign in to comment.