Skip to content

Commit b725f19

Browse files
committed
put unstyled lines in obj
1 parent 6e74bbe commit b725f19

File tree

2 files changed

+44
-121
lines changed

2 files changed

+44
-121
lines changed

README.md

Lines changed: 27 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -9,125 +9,40 @@
99

1010
Learn more about writing `.jsx` files for After Effects here: https://motiondeveloper.com/blog/write-expressions-external-files/
1111

12-
## Development
13-
14-
1. **Clone project locally**
15-
16-
```sh
17-
git clone repoUrl.git
18-
cd style-parser
19-
```
20-
21-
2. **Start Rollup**
22-
23-
Start Rollup in watch mode to automatically refresh your code as you make changes, by running:
24-
25-
```sh
26-
npm run watch
27-
```
28-
29-
_You can run also run a once off build:_ `npm run build`
30-
31-
3. **Edit the `src` files**
32-
33-
_The `index.ts` contains an example expression setup._
34-
35-
Any values exported from this file will be included in your library, for example:
36-
37-
```js
38-
export { someValue };
39-
```
40-
41-
4. **Import the `dist` file into After Effects**
42-
43-
Use the compiled output file as you would any other `.jsx` library. Any changes to the `src` files will be live updated, and After Effects will update the result of your expression.
44-
45-
5. **Distribute releases**
46-
47-
To distribute your output file using Github releases (via [Hub](https://github.com/github/hub)), use the command:
48-
49-
```sh
50-
npm run release
51-
```
52-
53-
This will use the GitHub CLI to create a new tag and release
54-
55-
The release version number is the `"version"` in `package.json`, and it will attach the `"main"` file to the release.
56-
57-
> You can add this version to the output file by placing the string `_npmVersion` in your code, which will be replaced with the version number in `package.json` at build time.
58-
59-
## After Effects API
60-
61-
> This template uses the [`expression-globals-typescript`](https://github.com/motiondeveloper/expression-globals-typescript) package to provide types for the expressions API.
62-
63-
### Classes
64-
65-
To create layers, compositions and properties, you can use the classes exported from the library. For example:
66-
6712
```ts
68-
import { Comp, Layer } from 'expression-globals-typescript';
69-
const thisComp = new Comp();
70-
const thisLayer = new Layer();
71-
```
72-
73-
To create properties (such as position or scale), you can use the `Property` class.
13+
type Line = Array<{ content: string; style: string }>;
7414

75-
```ts
76-
import { Property, Vector } from 'expression-globals-typescript';
77-
const thisProperty = new Property<Vector>([0, 100]);
15+
type parseStyles = (
16+
textString: string,
17+
parsers?: Array<{ matcher: RegExp; stylename: string }>
18+
) => Line[];
7819
```
7920

80-
> The `Property` constructor takes a value to set as the property value, and a type (`<>`) to set as the type for the property.
81-
82-
### After Effects Types
83-
84-
You can import After Effect's specific types such as `Color` and `Vector` from the package to properly type your expressions.
21+
## Example
8522

86-
#### _To see all the Types and Base Objects available, see the [`expression-globals-typescript`](https://github.com/motiondeveloper/expression-globals-typescript) source code._
23+
Expression:
8724

88-
## Testing
25+
```js
26+
const { parseStyles } = footage('style-parser.jsx').sourceData;
27+
const textString = `This will
28+
*be bold* and
29+
this will _be italics_`;
8930

90-
You can test your expression library code using [Jest](https://jestjs.io/), which comes pre-configured in this template repo.
91-
92-
You write tests in the `index.test.ts` file, importing the code you want to test from `index.ts`, for example:
93-
94-
```ts
95-
import { welcome } from './index';
96-
97-
test('returns correct welcome string', () => {
98-
expect(welcome('test')).toEqual('Welcome test!');
99-
});
31+
const parsed = parseStyles(textString);
10032
```
10133

102-
And then run the test suite:
103-
104-
```sh
105-
npm run test
34+
Results in:
35+
36+
```js
37+
[
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+
],
47+
];
10648
```
107-
108-
Which will run Jest in watch mode.
109-
110-
> You can learn more about testing using Jest from the [Jest docs](https://jestjs.io/docs/en/getting-started).
111-
112-
## Configuration
113-
114-
There a couple of files you may wish to change to reflect the content of your project:
115-
116-
- `package.json`:
117-
- `version`: The current version of the library, which is used for releases and added to `dist` files.
118-
- `main`: The build output file which will be attached to releases
119-
- `rollup.config.js`:
120-
- `input`: The source file to be built
121-
- `typescript()`: Custom typescript compiler options
122-
123-
## How
124-
125-
- [expression-globals-typescript](https://github.com/motiondeveloper/expression-globals-typescript) mocks the After Effects expressions API in typescript, so you can use global functions and objects such as `linear()` and `time`, while also providing expression specific types such as `Layer`.
126-
127-
- [Rollup](https://rollupjs.org/) is a lightweight module bundler that handles outputting the `.jsx` file via the plugins below.
128-
129-
- The Rollup [Typescript plugin](https://www.npmjs.com/package/@rollup/plugin-typescript) runs the TypeScript compiler
130-
131-
- The Rollup plugin [rollup-plugin-ae-jsx](https://www.npmjs.com/package/rollup-plugin-ae-jsx) transforms the JavaScript output into After Effects JSON (`.jsx`) compliant syntax
132-
133-
- Testing via [Jest](https://jestjs.io/), and [ts-jest](https://github.com/kulshekhar/ts-jest)

src/index.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ type ParsedSection = {
33
style: string;
44
};
55

6-
function parseStyles(inputText: string) {
6+
function parseStyles(
7+
inputText: string,
8+
customParsers: Array<{ matcher: RegExp; stylename: string }>
9+
) {
710
/** Split the input text into an array of lines */
811
const inputLines = inputText.split(/[\r\n\3]/g);
912

@@ -38,16 +41,21 @@ function parseStyles(inputText: string) {
3841
};
3942
}
4043

41-
/** Matches *content* */
42-
const BOLD_REGEX = new RegExp(/\*(.*)\*/);
43-
const parseBold = createLineParser(BOLD_REGEX, 'bold');
44+
const parsers = customParsers ?? [
45+
{ stylename: 'bold', matcher: new RegExp(/\*(.*)\*/) },
46+
{ stylename: 'italics', matcher: new RegExp(/_(.*)_/) },
47+
];
4448

45-
/** Matches _content_ */
46-
const ITALICS_REGEX = new RegExp(/_(.*)_/);
47-
const parseItalics = createLineParser(ITALICS_REGEX, 'italics');
49+
// Parse all the styles
50+
let styles: (string | ParsedSection | ParsedSection[])[] = inputLines;
51+
for (const { matcher, stylename } of parsers) {
52+
const parser = createLineParser(matcher, stylename);
53+
styles = styles.map(parser);
54+
}
4855

49-
const textStylesByLine = inputLines.map(parseBold).map(parseItalics);
50-
return textStylesByLine;
56+
return styles.map((line) =>
57+
typeof line === 'string' ? { content: line, style: 'regular' } : line
58+
);
5159
}
5260

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

0 commit comments

Comments
 (0)