-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (68 loc) · 3.57 KB
/
index.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
'use strict';
function valueWithThousandSeparators(number) { // number is a string
const value = Number(number);
if (isNaN(value))
return null;
if (value === 0) // We don't want to transform '000' to '0', for example
return null;
if (value > -6000 && value < 2200) // This might be a year, which doesn't take a thousand separator
return null;
let result = value.toLocaleString('fr-FR');
result = result.replace(/\s/g, ' ');
return result;
}
function insertThousandSeparatorsWhenStartOfInput(wholeMatch, number) {
const value = valueWithThousandSeparators(number);
if (value === null)
return wholeMatch;
return value;
}
function insertThousandSeparatorsWhenBlankBefore(wholeMatch, blank, number) {
const value = valueWithThousandSeparators(number);
if (value === null)
return wholeMatch;
return blank + value;
}
function replaceSpaceWithNbsp(wholematch, space_and_punctuation, space, punctuation, quote, offset, wholeinput) {
if (quote)
return wholematch;
return ` ${punctuation}`;
}
function replaceNumberAndSpaceWithNbsp(_wholeMatch, number, _space, symbol) {
return `${number} ${symbol}`;
}
function beautifyTextForFrench(src) {
// This code is not very great: ideally, we should use a real parser to analyze the text and not modify anything in an hyperlink's URL, a backslashed quote, block quotes, and probably embedded HTML.
// However, I don't have such an implementation now (and I suspect it would be near-impossible, since Markdown is quite ambiguous and not BNF).
// Fortunately, we don't need to deal with all possible uses of Markdown, but only with MemCheck. So this implementation relies on the presence of a space char, which proves that we are not in an URL.
// The biggest problem here is we insert thousand separators in years, which is wrong. I don't know yet how to solve that (eg question: "Combien de ml dans un l ?", answer: "1000").
let result = src;
// Insert thousand separators when we find a number after a space, or a space and a parenthesis
result = result.replace(/(\s\(?)(\d+)/g, insertThousandSeparatorsWhenBlankBefore);
// Insert thousand separators when we find a number at the begining of the text
result = result.replace(/^(\d+)/g, insertThousandSeparatorsWhenStartOfInput);
// White space before punctuation becomes nbsp if not in quote
result = result.replace(/(?<space_and_punctuation>(?<space> )(?<punctuation>\?|!|;|:))|(?<quote>`.+`)/g, replaceSpaceWithNbsp);
// Digit and white space before unit becomes nbsp
result = result.replace(/(\d)( )(€|mm|cm|dm|m|km|l|L|hl|bar|h\/km²|°|%)/g, replaceNumberAndSpaceWithNbsp);
return result;
}
/* exported convertMarkdown */
function convertMarkdown(src, beautifyForFrench) {
const actualText = beautifyForFrench ? beautifyTextForFrench(src) : src;
const converter = new showdown.Converter({ tables: true });
converter.setOption('openLinksInNewWindow', 'true');
converter.setOption('simplifiedAutoLink', 'true');
converter.setOption('simpleLineBreaks', 'true');
converter.setOption('noHeaderId', 'true'); // For size gain, even if minor
return converter.makeHtml(actualText);
}
function buttonClicked() {
const inputTextArea = document.getElementById('inputTextArea');
const inputText = inputTextArea.value;
var beautified = beautifyTextForFrench(inputText);
var output = convertMarkdown(beautified);
const renderedDiv = document.getElementById('renderedDiv');
renderedDiv.innerHTML = output;
}
module.exports = beautifyTextForFrench;