Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description key and description processing to sources tab #2747

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@ourworldindata/core-table/src/CoreColumnDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export interface CoreColumnDef extends ColumnColorScale {
attributionShort?: string // The Metadata V2 title disambiguation fragment for the producer
description?: string
descriptionShort?: string
descriptionProcessing?: string
descriptionKey?: string[]
descriptionFromProducer?: string
note?: string // Any internal notes the author wants to record for display in admin interfaces

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ const columnDefFromOwidVariable = (
datasetId,
datasetName,
descriptionShort,
descriptionProcessing,
descriptionKey,
descriptionFromProducer,
source,
origins,
Expand All @@ -578,6 +580,8 @@ const columnDefFromOwidVariable = (
shortUnit,
description,
descriptionShort,
descriptionProcessing,
descriptionKey,
descriptionFromProducer,
coverage,
datasetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

.datasource-wrapper ul,
.datasource-wrapper ol {
margin-left: 1.1rem;
margin-left: 0;
padding-left: 1rem;
margin-bottom: 0;
}

.datasource-wrapper table {
Expand Down
76 changes: 51 additions & 25 deletions packages/@ourworldindata/grapher/src/sourcesTab/SourcesTab.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Bounds,
DEFAULT_BOUNDS,
MarkdownTextWrap,
SimpleMarkdownText,
OwidOrigin,
uniq,
excludeNullish,
Expand All @@ -21,20 +21,17 @@ export interface SourcesTabManager {

// TODO: remove this component once all backported indicators
// etc have switched from HTML to markdown for their sources
const HtmlOrMarkdownText = (props: {
text: string
fontSize: number
}): JSX.Element => {
const HtmlOrMarkdownText = (props: { text: string }): JSX.Element => {
// check the text for closing a, li or p tags. If
// one is found, render using dangerouslySetInnerHTML,
// othewise use MarkdownTextWrap
const { text, fontSize } = props
// othewise use SimpleMarkdownText
const { text } = props
const htmlRegex = /<\/(a|li|p)>/
const match = text.match(htmlRegex)
if (match) {
return <span dangerouslySetInnerHTML={{ __html: text }} />
} else {
return <MarkdownTextWrap text={text} fontSize={fontSize} />
return <SimpleMarkdownText text={text} />
}
}

Expand Down Expand Up @@ -119,9 +116,8 @@ export class SourcesTab extends React.Component<{
<tr>
<td>Variable description</td>
<td>
<MarkdownTextWrap
<SimpleMarkdownText
text={column.def.descriptionShort}
fontSize={12}
/>
</td>
</tr>
Expand All @@ -132,9 +128,48 @@ export class SourcesTab extends React.Component<{
<tr>
<td>Variable description</td>
<td>
<HtmlOrMarkdownText
<SimpleMarkdownText
text={column.description}
fontSize={12}
/>
</td>
</tr>
) : null}
{column.def.descriptionKey &&
column.def.descriptionKey.length === 1 ? (
<tr>
<td>Key information</td>
<td>
<SimpleMarkdownText
text={column.def.descriptionKey[0]}
/>
</td>
</tr>
) : null}
{column.def.descriptionKey &&
column.def.descriptionKey.length > 1 ? (
<tr>
<td>Key information</td>
<td>
<ul>
{column.def.descriptionKey.map(
(info: string, index: number) => (
<li key={index}>
<SimpleMarkdownText
text={info}
/>
</li>
)
)}
</ul>
</td>
</tr>
) : null}
{column.def.descriptionProcessing ? (
<tr>
<td>Processing notes</td>
<td>
<SimpleMarkdownText
text={column.def.descriptionProcessing}
/>
</td>
</tr>
Expand All @@ -161,9 +196,8 @@ export class SourcesTab extends React.Component<{
<tr>
<td>Data published by</td>
<td>
<MarkdownTextWrap
<SimpleMarkdownText
text={publishedByArray[0]}
fontSize={12}
/>
</td>
</tr>
Expand All @@ -179,9 +213,8 @@ export class SourcesTab extends React.Component<{
index: number
) => (
<li key={index}>
<MarkdownTextWrap
<SimpleMarkdownText
text={citation}
fontSize={12}
/>
</li>
)
Expand All @@ -196,7 +229,6 @@ export class SourcesTab extends React.Component<{
<td>
<HtmlOrMarkdownText
text={source.dataPublisherSource}
fontSize={12}
/>
</td>
</tr>
Expand All @@ -205,10 +237,7 @@ export class SourcesTab extends React.Component<{
<tr>
<td>Link</td>
<td>
<HtmlOrMarkdownText
text={source.link}
fontSize={12}
/>
<HtmlOrMarkdownText text={source.link} />
</td>
</tr>
) : null}
Expand All @@ -222,10 +251,7 @@ export class SourcesTab extends React.Component<{
</table>
{source.additionalInfo && (
<p key={"additionalInfo"}>
<HtmlOrMarkdownText
text={source.additionalInfo}
fontSize={12}
/>
<HtmlOrMarkdownText text={source.additionalInfo} />
</p>
)}
</div>
Expand Down
36 changes: 36 additions & 0 deletions packages/@ourworldindata/utils/src/SimpleMarkdownText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from "react"
import { computed } from "mobx"
import {
IRToken,
parsimmonToTextTokens,
recursiveMergeTextTokens,
} from "./MarkdownTextWrap/MarkdownTextWrap.js"
import { MarkdownRoot, mdParser } from "./MarkdownTextWrap/parser.js"
type SimpleMarkdownTextProps = {
text: string
}

export class SimpleMarkdownText extends React.Component<SimpleMarkdownTextProps> {
@computed get text(): string {
return this.props.text
}

@computed get ast(): MarkdownRoot["children"] {
if (!this.text) return []
const result = mdParser.markdown.parse(this.props.text)
if (result.status) {
return result.value.children
}
return []
}

@computed get tokens(): IRToken[] {
const tokens = parsimmonToTextTokens(this.ast, {})
return recursiveMergeTextTokens(tokens)
}

render(): JSX.Element | null {
const { tokens } = this
return <>{tokens.map((token, index) => token.toHTML(index))}</>
}
}
2 changes: 2 additions & 0 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ export {
sumTextWrapHeights,
} from "./MarkdownTextWrap/MarkdownTextWrap.js"

export { SimpleMarkdownText } from "./SimpleMarkdownText.js"

export {
extractDetailsFromSyntax,
mdParser,
Expand Down
Loading