Skip to content

Commit 951ae96

Browse files
authored
Merge pull request #1 from navikt/update-pipeline
chore: 🤖 updated pipeline to include PRs
2 parents 3cc656d + ffcfc05 commit 951ae96

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

.github/workflows/pr.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: Test and build
22

33
on:
44
push:
5-
branches:
6-
- !master
5+
branches-ignore:
6+
- master
77

88
env:
99
CI: true

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Rule = {
2525
scope: RuleScope;
2626
regex: RegExp;
2727
parse(match: RegexMatch): ASTNode;
28-
react(node: ASTNode): ReactElementDescription;
28+
react(node: ASTNode, ast: AST): ReactElementDescription;
2929
}
3030

3131
const atRule = {
@@ -35,7 +35,7 @@ const atRule = {
3535
parse(match): ASTNode {
3636
return { name: 'atRule', content: [match.capture[0]] }
3737
},
38-
react(node: ASTNode): ReactElementDescription {
38+
react(node: ASTNode, ast: AST): ReactElementDescription {
3939
return { type: 'a', props: { href: `https://url.com?user=${Utils.getText(node)}` } }
4040
}
4141
};
@@ -48,7 +48,7 @@ import { Link } from 'react-router';
4848

4949
const customLinkRule: Rule = {
5050
...LinkRule,
51-
react(node: string | { name: string; content: AST }): ReactElementDescription {
51+
react(node: ASTNode, ast: AST): ReactElementDescription {
5252
return {type: Link, props: {className: 'paragraph-class', to: Utils.getText(node) }}
5353
}
5454
};

src/domain.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import { ReactNode, ComponentType } from 'react';
22

33
export type ASTNode =
44
| string
@@ -19,14 +19,14 @@ export type Rule = {
1919
scope: RuleScope;
2020
regex: RegExp;
2121
parse(match: RegexMatch): ASTNode;
22-
react(node: ASTNode): ReactElementDescription;
22+
react(node: ASTNode, ast: AST): ReactElementDescription;
2323
[key: string]: any;
2424
};
2525

2626
export interface ReactElementDescription {
27-
type: string | React.ComponentType<any>;
27+
type: string | ComponentType<any>;
2828
props?: { [key: string]: any };
29-
children?: Array<React.ReactNode>;
29+
children?: Array<ReactNode>;
3030
}
3131

3232
export type RegexMatch = {

src/parser.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,16 @@ function simplify(node: ASTNode): Array<ASTNode> {
8080
}
8181
}
8282

83-
function internalBuild(ruleMap: { [name: string]: Rule }, node: ASTNode, key: number): React.ReactNode {
83+
function internalBuild(ast: AST, ruleMap: { [name: string]: Rule }, node: ASTNode, key: number): React.ReactNode {
8484
if (typeof node === 'string') {
8585
return node;
8686
}
8787
const type = ruleMap[node.name];
88-
const element = type.react(node);
88+
const element = type.react(node, ast);
8989
const children =
9090
element.children?.length === 0 || node.content.length === 0
9191
? undefined
92-
: element.children || node.content.map((child, i) => internalBuild(ruleMap, child, i));
92+
: element.children || node.content.map((child, i) => internalBuild(ast, ruleMap, child, i));
9393
return createElement(element.type, {...element.props, key}, children);
9494
}
9595

@@ -112,6 +112,6 @@ export function parse(content: string, rules: Array<Rule>): AST {
112112

113113
export function build(ast: AST, rules: Array<Rule>): ReactElement<{}> {
114114
const ruleMap = rules.reduce((acc, rule) => ({...acc, [rule.name]: rule}), {});
115-
const nodes = ast.map((node, i) => internalBuild(ruleMap, node, i));
115+
const nodes = ast.map((node, i) => internalBuild(ast, ruleMap, node, i));
116116
return createElement(Fragment, {}, nodes);
117117
}

test/rules.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('rules', () => {
2626
name: 'Linebreak',
2727
content: []
2828
});
29-
expect(LinebreakRule.react('')).toEqual({
29+
expect(LinebreakRule.react('', [])).toEqual({
3030
type: 'br'
3131
});
3232
});
@@ -46,7 +46,7 @@ describe('rules', () => {
4646
name: 'Paragraph',
4747
content: ['Content here']
4848
});
49-
expect(ParagraphRule.react('')).toEqual({
49+
expect(ParagraphRule.react('', [])).toEqual({
5050
type: 'p'
5151
});
5252
});
@@ -80,7 +80,7 @@ describe('rules', () => {
8080
name: 'Highlight',
8181
content: ['Content here']
8282
});
83-
expect(HighlightRule.react('')).toEqual({
83+
expect(HighlightRule.react('', [])).toEqual({
8484
type: 'em'
8585
});
8686
});
@@ -112,7 +112,7 @@ describe('rules', () => {
112112
name: 'DynamicHighlight',
113113
content: ['Content here']
114114
});
115-
expect(rule.react('')).toEqual({
115+
expect(rule.react('', [])).toEqual({
116116
type: 'em'
117117
});
118118
});
@@ -142,7 +142,7 @@ describe('rules', () => {
142142
name: 'Bold',
143143
content: ['Content here']
144144
});
145-
expect(BoldRule.react('')).toEqual({
145+
expect(BoldRule.react('', [])).toEqual({
146146
type: 'b'
147147
});
148148
});
@@ -173,7 +173,7 @@ describe('rules', () => {
173173
name: 'Link',
174174
content: ['Content here']
175175
});
176-
expect(LinkRule.react({name: 'Link', content: ['url.com']})).toEqual({
176+
expect(LinkRule.react({name: 'Link', content: ['url.com']}, [])).toEqual({
177177
type: 'a',
178178
props: {target: '_blank', rel: 'noopener', href: 'https://url.com'}
179179
});
@@ -188,7 +188,7 @@ describe('rules', () => {
188188
'.com'
189189
]
190190
};
191-
expect(LinkRule.react(astnode)).toMatchObject({
191+
expect(LinkRule.react(astnode, [])).toMatchObject({
192192
type: 'a',
193193
props: {
194194
href: 'https://www.domain.com',
@@ -208,7 +208,7 @@ describe('rules', () => {
208208
'.com'
209209
]
210210
};
211-
expect(LinkRule.react(astnode)).toMatchObject({
211+
expect(LinkRule.react(astnode, [])).toMatchObject({
212212
type: 'a',
213213
props: {
214214
href: 'http://www.domain.com',

0 commit comments

Comments
 (0)