-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor html parsing and template import logic
- Loading branch information
Showing
3 changed files
with
285 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { | ||
isDefined, | ||
isNotDefined, | ||
} from '@togglecorp/fujs'; | ||
import { type CellRichTextValue } from 'exceljs'; | ||
|
||
export interface Plugin { | ||
tag: string, | ||
transformer: (token: string, richText: CellRichTextValue['richText'][number]) => CellRichTextValue['richText'][number], | ||
} | ||
|
||
const boldPlugin: Plugin = { | ||
tag: 'b', | ||
transformer: (_: string, richText) => ({ | ||
...richText, | ||
font: { | ||
...richText.font, | ||
bold: true, | ||
}, | ||
}), | ||
}; | ||
const italicsPlugin: Plugin = { | ||
tag: 'i', | ||
transformer: (_: string, richText) => ({ | ||
...richText, | ||
font: { | ||
...richText.font, | ||
italic: true, | ||
}, | ||
}), | ||
}; | ||
const underlinePlugin: Plugin = { | ||
tag: 'u', | ||
transformer: (_: string, richText) => ({ | ||
...richText, | ||
font: { | ||
...richText.font, | ||
underline: true, | ||
}, | ||
}), | ||
}; | ||
|
||
/** | ||
* Convert subset of html into excel's richtext format | ||
* @param value string with or without html tags | ||
*/ | ||
export function parsePseudoHtml( | ||
value: undefined, | ||
extraPlugins?: Plugin[], | ||
): undefined; | ||
export function parsePseudoHtml( | ||
value: string, | ||
extraPlugins?: Plugin[], | ||
): string | CellRichTextValue | ||
export function parsePseudoHtml( | ||
value: string | undefined, | ||
extraPlugins?: Plugin[], | ||
): string | CellRichTextValue | undefined | ||
export function parsePseudoHtml( | ||
value: string | undefined, | ||
extraPlugins: Plugin[] = [], | ||
): string | CellRichTextValue | undefined { | ||
if (isNotDefined(value)) { | ||
return value; | ||
} | ||
|
||
const plugins: Plugin[] = [ | ||
boldPlugin, | ||
italicsPlugin, | ||
underlinePlugin, | ||
...extraPlugins, | ||
]; | ||
|
||
const tagRegex = RegExp(`</?(?:${plugins.map((p) => p.tag).join('|')})>`); | ||
const tokens = value.split(tagRegex); | ||
if (tokens.length === 1) { | ||
return value; | ||
} | ||
|
||
const openTagRegex = RegExp(`<(?:${plugins.map((p) => p.tag).join('|')})>`); | ||
const closeTagRegex = RegExp(`</(?:${plugins.map((p) => p.tag).join('|')})>`); | ||
|
||
const stack: string[] = []; | ||
const richText = tokens.map((token) => { | ||
if (token.match(openTagRegex)) { | ||
stack.push(token); | ||
return undefined; | ||
} | ||
if (token.match(closeTagRegex)) { | ||
// TODO: Check correctness by checking closeTag with last openTag | ||
stack.pop(); | ||
return undefined; | ||
} | ||
|
||
const richTextItem: CellRichTextValue['richText'][number] = plugins | ||
.filter((plugin) => stack.includes(`<${plugin.tag}>`)) | ||
.reduce( | ||
(acc, plugin) => plugin.transformer(token, acc), | ||
{ text: token }, | ||
); | ||
return richTextItem; | ||
}).filter(isDefined); | ||
|
||
// TODO: Check correctness to check that stack is empty | ||
return { richText }; | ||
} |
Oops, something went wrong.