Skip to content

Commit ff2a1c6

Browse files
committed
add other style methods
1 parent 17c763f commit ff2a1c6

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

README.md

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

1212
```ts
13+
type StyleResult = { content: string; style: string; line: number }[];
14+
15+
/** Parse a markdown-ish string*/
1316
type parseStyles = (
1417
textString: string,
1518
parsers?: Array<{ matcher: RegExp; stylename: string }>
16-
) => { content: string; style: string; line: number }[];
19+
) => StyleResult;
20+
21+
/** Style the given word indexes */
22+
type styleByIndex = (textString: string, {
23+
boldIndexes: number[],
24+
italicsIndexes: number[]
25+
}) => StyleResult;
26+
27+
/** Style the given words */
28+
type styleBySearch = (textString: string, {
29+
boldString?: string,
30+
italicsString?: string,
31+
}) => StyleResult;
1732
```
1833

1934
## Example
@@ -42,3 +57,7 @@ Results in:
4257
{ content: 'be italics', style: 'italics', line: 2 },
4358
];
4459
```
60+
61+
## Limitations
62+
63+
Currently you need to style whole words in each method, otherwise you'll get unnecessary spaces inserted.

src/index.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type ParsedSection = {
77
function parseStyles(
88
inputText: string,
99
customParsers: Array<{ matcher: RegExp; stylename: string }> = []
10-
) {
10+
): ParsedSection[] {
1111
/** Split the input text into an array of lines */
1212
const inputLines = inputText.split(/[\r\n\3]/g);
1313

@@ -64,6 +64,84 @@ function parseStyles(
6464
return parsedStyles.flat();
6565
}
6666

67+
function styleByIndex(
68+
inputText: string,
69+
{
70+
boldIndexes,
71+
italicsIndexes,
72+
}: { boldIndexes: number[]; italicsIndexes: number[] }
73+
): ParsedSection[] {
74+
const inputLines = inputText.split(/[\r\n\3]/g);
75+
const words = inputLines.flatMap((line, index) =>
76+
line
77+
.trim()
78+
.split(' ')
79+
.map((word) => ({ content: word, line: index }))
80+
);
81+
82+
const wordsWithStyles = words.map((word, index) => {
83+
let style = 'regular';
84+
if (boldIndexes.includes(index)) style = 'bold';
85+
if (italicsIndexes.includes(index)) style = 'italics';
86+
return {
87+
...word,
88+
style,
89+
};
90+
});
91+
92+
return wordsWithStyles;
93+
}
94+
95+
function styleBySearch(
96+
inputText: string,
97+
{
98+
boldString,
99+
italicsString,
100+
}: { boldString?: string; italicsString?: string } = {}
101+
): ParsedSection[] {
102+
let regex;
103+
const italics = italicsString ? italicsString.trim() : '';
104+
const bold = boldString ? boldString.trim() : '';
105+
if (boldString && italicsString) {
106+
regex = new RegExp(`(${bold})|(${italics})`, 'g');
107+
} else if (boldString) {
108+
regex = new RegExp(`(${bold})`, 'g');
109+
} else if (italicsString) {
110+
regex = new RegExp(`(${italics})`, 'g');
111+
}
112+
113+
if (!regex) {
114+
return [
115+
{
116+
content: inputText,
117+
style: 'regular',
118+
line: 0,
119+
},
120+
];
121+
}
122+
123+
const inputLines = inputText.split(/[\r\n\3]/g);
124+
125+
return inputLines.flatMap((line, index) => {
126+
return line
127+
.split(regex)
128+
.filter(Boolean)
129+
.map((part) => {
130+
const style =
131+
part === boldString
132+
? 'bold'
133+
: part === italicsString
134+
? 'italics'
135+
: 'regular';
136+
return {
137+
content: part.trim(),
138+
style,
139+
line: index,
140+
};
141+
});
142+
});
143+
}
144+
67145
const defaultFontMap = {
68146
bold: 'Arial-BoldMT',
69147
italics: 'Arial-ItalicMT',
@@ -74,4 +152,4 @@ const defaultFontMap = {
74152
const version: string = '_npmVersion';
75153

76154
// Export values to appear in jsx files
77-
export { parseStyles, defaultFontMap, version };
155+
export { parseStyles, styleByIndex, styleBySearch, defaultFontMap, version };

0 commit comments

Comments
 (0)