This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
language-checker.js
120 lines (101 loc) · 3.23 KB
/
language-checker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'use strict';
import React from 'react';
import PropTypes from 'prop-types';
import {
Icon,
Typography,
Subheading,
SectionHeading,
Paragraph
} from '@contentful/forma-36-react-components';
import { documentToPlainTextString } from '@contentful/rich-text-plain-text-renderer';
import alex from 'alex';
import { NoIssues } from './no-issues.js';
import { Message } from './message.js';
function checkContent(isRichText, value, config = {}) {
const { messages } = isRichText
? alex.text(documentToPlainTextString(value), config)
: alex.markdown(value, config);
return messages;
}
export class LanguageChecker extends React.Component {
constructor() {
super();
this.state = { messageMap: {} };
}
componentDidMount() {
const { entry, fieldsToCheck, alexConfig } = this.props;
fieldsToCheck.forEach(fieldDefinition => {
const fieldId = fieldDefinition.id;
const field = entry.fields[fieldId];
const currentValue = field.getValue();
const isRichText = fieldDefinition.type === 'RichText';
if (currentValue) {
const messages = checkContent(isRichText, currentValue, alexConfig);
this.setState(({ messageMap }) => {
return { messageMap: { ...messageMap, [fieldId]: messages } };
});
}
field.onValueChanged(value => {
const messages = checkContent(isRichText, value, alexConfig);
this.setState(({ messageMap }) => {
return { messageMap: { ...messageMap, [fieldId]: messages } };
});
});
});
}
render() {
const messageEntries = Object.entries(this.state.messageMap);
const { fieldsToCheck } = this.props;
const messageCount = messageEntries.reduce((count, [, messages]) => {
return count + messages.length;
}, 0);
if (messageCount === 0) {
return <NoIssues />;
}
return (
<React.Fragment>
<Typography>
<Subheading className="align-center">
<Icon icon="Warning" color="warning" className="f36-margin-right--xs" />
Issues found
</Subheading>
<Paragraph>There are some issues with the content in the following fields:</Paragraph>
</Typography>
{messageEntries.map(([fieldId, messages]) => {
if (messages.length === 0) {
return null;
}
const fieldDefiniton = fieldsToCheck.find(({ id }) => id === fieldId);
return (
<div key={fieldId}>
<SectionHeading className="f36-margin-bottom--m">
{fieldDefiniton.name}
</SectionHeading>
<ul className="warning-list f36-margin-bottom--m">
{messages.map((message, index) => (
<Message message={message} key={index} />
))}
</ul>
</div>
);
})}
</React.Fragment>
);
}
}
LanguageChecker.propTypes = {
fieldsToCheck: PropTypes.array.isRequired,
entry: PropTypes.object.isRequired,
alexConfig: PropTypes.shape({
noBinary: PropTypes.bool,
profanitySureness: PropTypes.oneOf([0, 1, 2]),
allow: PropTypes.arrayOf(PropTypes.string)
})
};
LanguageChecker.defaultProps = {
alexConfig: {
profanitySureness: 0,
allow: []
}
};