aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/frontend/markdown.js
blob: 3d26f4ce303be7749bffe07a098d142e67faf11c (plain)
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
// Parses a string as markdown (markdown.js)
HashOverConstructor.prototype.markdown = {
	blockCodeRegex: /```([\s\S]+?)```/g,
	inlineCodeRegex: /(^|[^a-z0-9`])`([^`]+?[\s\S]+?)`([^a-z0-9`]|$)/ig,
	blockCodeMarker: /CODE_BLOCK\[([0-9]+)\]/g,
	inlineCodeMarker: /CODE_INLINE\[([0-9]+)\]/g,

	// Array for inline code and code block markers
	codeMarkers: {
		block: { marks: [], count: 0 },
		inline: { marks: [], count: 0 }
	},

	// Markdown patterns to search for
	markdownSearch: [
		/\*\*([^ *])([\s\S]+?)([^ *])\*\*/g,
		/\*([^ *])([\s\S]+?)([^ *])\*/g,
		/(^|\W)_([^_]+?[\s\S]+?)_(\W|$)/g,
		/__([^ _])([\s\S]+?)([^ _])__/g,
		/~~([^ ~])([\s\S]+?)([^ ~])~~/g
	],

	// HTML replacements for markdown patterns
	markdownReplace: [
		'<strong>$1$2$3</strong>',
		'<em>$1$2$3</em>',
		'$1<u>$2</u>$3',
		'<u>$1$2$3</u>',
		'<s>$1$2$3</s>'
	],

	// Replaces markdown for inline code with a marker
	codeReplace: function (fullTag, first, second, third, display)
	{
		var markName = 'CODE_' + display.toUpperCase ();
		var markCount = this.codeMarkers[display].count++;

		if (display !== 'block') {
			var codeMarker = first + markName + '[' + markCount + ']' + third;
			this.codeMarkers[display].marks[markCount] = this.parent.EOLTrim (second);
		} else {
			var codeMarker = markName + '[' + markCount + ']';
			this.codeMarkers[display].marks[markCount] = this.parent.EOLTrim (first);
		}

		return codeMarker;
	},

	parse: function (string)
	{
		// Reference to this object
		var markdown = this;

		// Reset marker arrays
		this.codeMarkers = {
			block: { marks: [], count: 0 },
			inline: { marks: [], count: 0 }
		};

		// Replace code blocks with markers
		string = string.replace (this.blockCodeRegex, function (fullTag, first, second, third) {
			return markdown.codeReplace (fullTag, first, second, third, 'block');
		});

		// Break string into paragraphs
		var paragraphs = string.split (this.parent.regex.paragraphs);

		// Run through each paragraph replacing markdown patterns
		for (var i = 0, il = paragraphs.length; i < il; i++) {
			// Replace code tags with marker text
			paragraphs[i] = paragraphs[i].replace (this.inlineCodeRegex, function (fullTag, first, second, third) {
				return markdown.codeReplace (fullTag, first, second, third, 'inline');
			});

			// Perform each markdown regular expression on the current paragraph
			for (var r = 0, rl = this.markdownSearch.length; r < rl; r++) {
				// Replace markdown patterns
				paragraphs[i] = paragraphs[i].replace (this.markdownSearch[r], this.markdownReplace[r]);
			}

			// Return the original markdown code with HTML replacement
			paragraphs[i] = paragraphs[i].replace (this.inlineCodeMarker, function (marker, number) {
				return '<code class="hashover-inline">' + markdown.codeMarkers.inline.marks[number] + '</code>';
			});
		}

		// Join paragraphs
		string = paragraphs.join (this.parent.setup['server-eol'] + this.parent.setup['server-eol']);

		// Replace code block markers with original markdown code
		string = string.replace (this.blockCodeMarker, function (marker, number) {
			return '<code>' + markdown.codeMarkers.block.marks[number] + '</code>';
		});

		return string;
	}
};