-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
8 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,60 @@ | ||
import { expect, test } from 'vitest'; | ||
import { transformLists, replacePageTags as transformPageTags, removeImageTags } from './storybook'; | ||
|
||
function tokensOf(str: string) { | ||
return str.split(/\s+/).filter((token) => token.length); | ||
} | ||
|
||
test('replace page tags', () => { | ||
const input = 'abc \\page 41 xyz \\page 45 122'; | ||
const expected = 'abc \\c 41 xyz \\c 45 122'; | ||
const result = transformPageTags(input); | ||
expect(tokensOf(result)).toEqual(tokensOf(expected)); | ||
}); | ||
|
||
// For now, get image data from config.js (may change this in the future) | ||
test('remove img tags', () => { | ||
const input = 'abc \\img img1.jpeg efg \\img image-2.jpeg \\img image_with_underscores.jpeg'; | ||
const expected = 'abc efg'; | ||
const result = removeImageTags(input); | ||
expect(tokensOf(result)).toEqual(tokensOf(expected)); | ||
}); | ||
|
||
test('convert unordered list to milestones', () => { | ||
const input = ` | ||
\\m Some content | ||
\\zuli1 One | ||
\\zuli1 Two \\zuli1 Three in a row! | ||
\\b | ||
`; | ||
const expected = ` | ||
\\m Some content | ||
\\zuli1-s\\* One \\zuli1-e\\* | ||
\\zuli1-s\\* Two \\zuli1-e\\* | ||
\\zuli1-s\\* Three in a row! \\zuli1-e\\* | ||
\\b | ||
`; | ||
const result = transformLists(input); | ||
expect(tokensOf(result)).toEqual(tokensOf(expected)); | ||
}); | ||
|
||
test('convert ordered list to milestones', () => { | ||
const input = ` | ||
\\m Some content | ||
\\zon1 10 | ||
\\zoli1 One | ||
\\zoli1 Two \\zoli1 Three in a row! | ||
\\b | ||
`; | ||
const expected = ` | ||
\\m Some content | ||
\\zon1-s |start="10"\\* | ||
\\zoli1-s\\* One \\zoli1-e\\* | ||
\\zoli1-s\\* Two \\zoli1-e\\* | ||
\\zoli1-s\\* Three in a row! \\zoli1-e\\* | ||
\\zon1-e\\* | ||
\\b | ||
`; | ||
const result = transformLists(input); | ||
expect(tokensOf(result)).toEqual(tokensOf(expected)); | ||
}); |
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,32 @@ | ||
/** | ||
* Replace page tags with chapters | ||
*/ | ||
export function replacePageTags(usfm: string): string { | ||
return usfm.replace(/\\page\s+(\d+)/g, '\\c $1'); | ||
} | ||
|
||
/** | ||
* Remove img tags | ||
* | ||
* For now, get images from config (may change this later) | ||
*/ | ||
export function removeImageTags(usfm: string): string { | ||
return usfm.replace(/\\img\s+\S+/g, ''); | ||
} | ||
|
||
/** | ||
* Convert list tags to milestones | ||
*/ | ||
export function transformLists(usfm: string): string { | ||
return usfm | ||
.replace(/\\zuli1\s+([^\\]*)/g, '\\zuli1-s\\* $1 \\zuli1-e\\* ') | ||
.replace(/\\zon1 (\d+)(([^\\]|\\zoli)*)/g, '\\zon1-s |start="$1"\\* $2 \\zon1-e\\* ') | ||
.replace(/\\zoli1\s+([^\\]*)/g, '\\zoli1-s\\* $1 \\zoli1-e\\* '); | ||
} | ||
|
||
export function convertStorybookElements(usfm: string) { | ||
usfm = replacePageTags(usfm); | ||
usfm = removeImageTags(usfm); | ||
usfm = transformLists(usfm); | ||
return usfm; | ||
} |