Skip to content

Commit

Permalink
✨ add list rendering, tweak line breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Nov 7, 2023
1 parent e25aaa8 commit 3e3744f
Showing 1 changed file with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ export class MarkdownTextWrap extends React.Component<MarkdownTextWrapProps> {
}
}
@computed get text(): string {
// NOTE: ❗Here we deviate from the normal markdown spec. We replace \n with \\\n to make sure that single \n are treated as
// NOTE: ❗Here we deviate from the normal markdown spec. We replace \n with <SPACE><SPACE>\n to make sure that single \n are treated as
// actual line breaks but only if none of the other markdown line break rules apply.
// This is a bit different to how markdown usually works but we have a substantial
// amount of legacy charts that use newlines in this way and it seems that it is
Expand All @@ -536,17 +536,13 @@ export class MarkdownTextWrap extends React.Component<MarkdownTextWrapProps> {
// - \n\n is always a new paragraph
// - Two spaces before \n is a line break (this rule is not entirely checked as we only check for a single space)
// - A backslash before \n is a line break
// The regex tries to find \n that do not belong to any of the three cases above and if none of those apply we
// replace the \n in the matched 3 characters with a backslash preceding \n to make sure it will be a line break.
// return baseText.replaceAll(/[^\n \\]\n[^\n]/g, (match) =>
// match.replace("\n", "\\\n")
// )
// The code below normalizes all cases to <SPACE><SPACE>\n which will lead to them surviving the markdown parsing
let text = baseText.trim()
text = text.replaceAll("\n\n", "@@LINEBREAK@@")
text = text.replaceAll("\\\n", "@@LINEBREAK@@")
text = text.replaceAll(" \n", "@@LINEBREAK@@")
text = text.replaceAll("\n", "\\\n")
text = text.replaceAll("@@LINEBREAK@@", "\\\n")
text = text.replaceAll("\n", " \n")
text = text.replaceAll("@@LINEBREAK@@", " \n")
return text
}
@computed get detailsOrderedByReference(): Set<string> {
Expand Down Expand Up @@ -710,15 +706,8 @@ export function convertMarkdownToIRTokens(
markdown: string,
fontParams?: IRFontParams
): IRToken[] {
const ast = fromMarkdown(markdown)
return convertMarkdownRootToIRTokens(ast, fontParams)
}

function convertMarkdownRootToIRTokens(
node: Root,
fontParams?: IRFontParams
): IRToken[] {
return node.children.flatMap((item) =>
const ast: Root = fromMarkdown(markdown)
return ast.children.flatMap((item: Content) =>
convertMarkdownNodeToIRTokens(item, fontParams)
)
}
Expand Down Expand Up @@ -839,9 +828,18 @@ function convertMarkdownNodeToIRTokens(
type: "list",
},
(item) => {
return item.children.flatMap((child) =>
convertMarkdownNodeToIRTokens(child, fontParams)
)
if (item.ordered)
return item.children.flatMap((child, index) => [
new IRLineBreak(),
new IRText(`${index + 1}) `, fontParams),
...convertMarkdownNodeToIRTokens(child, fontParams),
])
else
return item.children.flatMap((child, index) => [

Check warning on line 838 in packages/@ourworldindata/components/src/MarkdownTextWrap/MarkdownTextWrap.tsx

View workflow job for this annotation

GitHub Actions / eslint

'index' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 838 in packages/@ourworldindata/components/src/MarkdownTextWrap/MarkdownTextWrap.tsx

View workflow job for this annotation

GitHub Actions / eslint

'index' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 838 in packages/@ourworldindata/components/src/MarkdownTextWrap/MarkdownTextWrap.tsx

View workflow job for this annotation

GitHub Actions / eslint

'index' is defined but never used. Allowed unused args must match /^_/u
new IRLineBreak(),
new IRText(`• `, fontParams),
...convertMarkdownNodeToIRTokens(child, fontParams),
])
}
)
.with(
Expand Down

0 comments on commit 3e3744f

Please sign in to comment.