Skip to content

Commit ec2af4e

Browse files
committed
return flat array with line numbers
1 parent dfbd779 commit ec2af4e

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
Learn more about writing `.jsx` files for After Effects here: https://motiondeveloper.com/blog/write-expressions-external-files/
1111

1212
```ts
13-
type Line = Array<{ content: string; style: string }>;
14-
1513
type parseStyles = (
1614
textString: string,
1715
parsers?: Array<{ matcher: RegExp; stylename: string }>
18-
) => Line[];
16+
) => { content: string; style: string; line: number }[];
1917
```
2018

2119
## Example
@@ -35,14 +33,12 @@ Results in:
3533

3634
```js
3735
[
38-
[{ content: 'This will', style: 'regular' }],
39-
[
40-
{ content: 'be bold', style: 'bold' },
41-
{ content: 'and', style: 'regular' },
42-
],
43-
[
44-
{ content: 'this will', style: 'regular' },
45-
{ content: 'be italics', style: 'italics' },
46-
],
36+
{ content: 'This will', style: 'regular', line: 0 },
37+
38+
{ content: 'be bold', style: 'bold', line: 1 },
39+
{ content: 'and', style: 'regular', line: 1 },
40+
41+
{ content: 'this will', style: 'regular', line: 2 },
42+
{ content: 'be italics', style: 'italics', line: 2 },
4743
];
4844
```

src/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
type ParsedSection = {
22
content: string;
33
style: string;
4+
line: number;
45
};
56

67
function parseStyles(
@@ -12,7 +13,7 @@ function parseStyles(
1213

1314
/** Creates a function that turns lines into a style tree based on a given regex */
1415
function createLineParser(matcher: RegExp, stylename: string) {
15-
return (line: string | ParsedSection | ParsedSection[]) => {
16+
return (line: string | ParsedSection | ParsedSection[], index: number) => {
1617
// Section has already been parsed
1718
if (typeof line !== 'string') return line;
1819
const captureGroups = matcher.exec(line);
@@ -31,10 +32,10 @@ function parseStyles(
3132
if (content === '') return null;
3233
// The content is what matched the regex, it's the styled content
3334
if (content === captureGroups[1]) {
34-
return { content, style: stylename };
35+
return { line: index, content, style: stylename };
3536
}
3637
// It's unstyled
37-
return { content, style: 'regular' };
38+
return { line: index, content, style: 'regular' };
3839
})
3940
.filter(Boolean) as ParsedSection[]
4041
);
@@ -47,15 +48,19 @@ function parseStyles(
4748
];
4849

4950
// Parse all the styles
50-
let styles: (string | ParsedSection | ParsedSection[])[] = inputLines;
51+
let styles = inputLines;
5152
for (const { matcher, stylename } of parsers) {
5253
const parser = createLineParser(matcher, stylename);
5354
styles = styles.map(parser);
5455
}
5556

56-
return styles.map((line) =>
57-
typeof line === 'string' ? { content: line, style: 'regular' } : line
57+
const parsedStyles = styles.map((line, index) =>
58+
typeof line === 'string'
59+
? [{ content: line, style: 'regular', line: index }]
60+
: line
5861
);
62+
63+
return parsedStyles.flat();
5964
}
6065

6166
// '_npmVersion' is replaced with value from package.json

0 commit comments

Comments
 (0)