<commit_before>// ----- Setup ----- //

var HEADINGS = {
	h1: '# ',
	h2: '## ',
	h3: '### ',
	h4: '#### ',
	h5: '##### ',
	h6: '###### '
};

var SYNTAX = {

	bullet: { before: '- ', after: '', caretMove: null },
	italics: { before: '*', after: '*', caretMove: 1 },
	bold: { before: '**', after: '**', caretMove: 2 },
	code: { before: '```\n', after: '\n```', caretMove: 4 },
	eq_inline: { before: '$', after: '$', caretMove: 1 },
	eq_standalone: { before: '$$\n', after: '\n$$', caretMove: 3 },

};


// ----- Functions ----- //

// Inserts a snippet into the editor.
function insert (symbolBefore, symbolAfter, editor) {

	var currentContent = editor.getContent();
	var selection = editor.getSelection();

	var contentBefore = currentContent.substring(0, selection.start);
	var contentSelected = currentContent.substring(
		selection.start, selection.end);
	var contentAfter = currentContent.substring(selection.end);

	var newContent = contentBefore + symbolBefore + contentSelected +
		symbolAfter + contentAfter;

	editor.setContent(newContent);
	editor.focus();

}

// Adds headings to content, H1 through H6.
function setupHeadings (toolbar, editor) {

	function addHeading () {
		var snippet = HEADINGS[this.name];
		insert(snippet, '', editor);
	}

	for (var heading in HEADINGS) {
		toolbar.action(heading, addHeading);
	}

}

// Moves the editor caret position.
function moveCaret (caretMove, editor) {

	if (caretMove) {

		var selection = editor.getSelection();
		selection.start -= caretMove;
		selection.end -= caretMove;
		editor.setSelection(selection);

	}

}

// Inserts a piece of syntax and moves the caret.
function insertSyntax (syntax, editor) {

	return function () {
		insert(syntax.before, syntax.after, editor);
		moveCaret(syntax.caretMove, editor);
	};

}

// Sets up markdown syntax insertion.
function setupSyntax (toolbar, editor) {

	for (var item in SYNTAX) {
		var syntaxPiece = SYNTAX[item];
		toolbar.action(item, insertSyntax(syntaxPiece, editor));
	}

}

// Sets up handling of tab key.
function setupTab (editor) {

	editor.keyDown(onTab);

	function onTab (keyEvent) {

		if (keyEvent.keyCode === 9) {

			var selection = editor.getSelection();

			insert('    ', '', editor);
			editor.setSelection(selection);
			moveCaret(-4, editor);

			keyEvent.preventDefault();

		}

	}

}

// Formats a link to make sure that it is web compatible.
function formatLink (link) {

	if (link.substr(0, 4) !== 'http') {
		link = 'https://' + link;
	}

	return link;

}

// Inserts a link into the editor.
function insertLink (editor, linkText) {

	var linkSyntax = {
		before: '[label',
		after: `](${linkText})`,
		caretMove: null
	};

	insertSyntax(linkSyntax, editor)();<commit_msg>Fixed bug in caret movement after insert.<commit_after>// ----- Setup ----- //

var HEADINGS = {
	h1: '# ',
	h2: '## ',
	h3: '### ',
	h4: '#### ',
	h5: '##### ',
	h6: '###### '
};

var SYNTAX = {

	bullet: { before: '- ', after: '', caretMove: 2 },
	italics: { before: '*', after: '*', caretMove: 1 },
	bold: { before: '**', after: '**', caretMove: 2 },
	code: { before: '```\n', after: '\n```', caretMove: 4 },
	eq_inline: { before: '$', after: '$', caretMove: 1 },
	eq_standalone: { before: '$$\n', after: '\n$$', caretMove: 3 },

};


// ----- Functions ----- //

// Inserts a snippet into the editor.
function insert (symbolBefore, symbolAfter, editor) {

	var currentContent = editor.getContent();
	var selection = editor.getSelection();

	var contentBefore = currentContent.substring(0, selection.start);
	var contentSelected = currentContent.substring(
		selection.start, selection.end);
	var contentAfter = currentContent.substring(selection.end);

	var newContent = contentBefore + symbolBefore + contentSelected +
		symbolAfter + contentAfter;

	editor.setContent(newContent);

	return selection;

}

// Adds headings to content, H1 through H6.
function setupHeadings (toolbar, editor) {

	function addHeading () {

		var snippet = HEADINGS[this.name];
		var selection = insert(snippet, '', editor);
		moveCaret(selection, snippet.length, editor);

	}

	for (var heading in HEADINGS) {
		toolbar.action(heading, addHeading);
	}

}

// Moves the editor caret position.
function moveCaret (selection, caretMove, editor) {

	editor.focus();

	if (caretMove) {

		selection.start += caretMove;
		selection.end += caretMove;
		editor.setSelection(selection);

	}

}

// Inserts a piece of syntax and moves the caret.
function insertSyntax (syntax, editor) {

	return function () {

		var selection = insert(syntax.before, syntax.after, editor);
		moveCaret(selection, syntax.caretMove, editor);

	};

}

// Sets up markdown syntax insertion.
function setupSyntax (toolbar, editor) {

	for (var item in SYNTAX) {
		var syntaxPiece = SYNTAX[item];
		toolbar.action(item, insertSyntax(syntaxPiece, editor));
	}

}

// Sets up handling of tab key.
function setupTab (editor) {

	editor.keyDown(onTab);

	function onTab (keyEvent) {

		if (keyEvent.keyCode === 9) {

			var selection = insert('    ', '', editor);
			moveCaret(selection, -4, editor);

			keyEvent.preventDefault();

		}

	}

}

// Formats a link to make sure that it is web compatible.
function formatLink (link) {

	if (link.substr(0, 4) !== 'http') {
		link = 'https://' + link;
	}

	return link;

}

// Inserts a link into the editor.
function insertLink (editor, linkText) {

	var linkSyntax = {
		before: '[label',
		after: `](${linkText})`,
		caretMove: null
	};

	insertSyntax(linkSyntax, editor)();
{'fraction_non_alphanumeric': 0.16842872767043382, 'fraction_numerical': 0.0067444403937294934, 'mean_word_length': 3.7058319039451115, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4302099', 'n_tokens_mistral': 1966, 'n_tokens_neox': 1779, 'n_words': 486}