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

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


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

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

	var editArea = editor.editArea;
	var selectStart = editArea.selectionStart;
	var selectEnd = editArea.selectionEnd;

	editArea.value = editArea.value.substring(0, selectStart) + snippet +
		editArea.value.substring(selectEnd);

	editArea.focus();
	editor.updatePreview();

}

// Adds headings to content.
function addHeadings (document, editor) {

	var headingButtons = document.getElementsByClassName('heading_button');

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

	for (var i = headingButtons.length - 1; i >= 0; i--) {
		headingButtons[i].addEventListener('click', addHeading);
	}

}

// Adds bullet points to content.
function addBullets (document, editor) {

	var bulletButton = document.getElementById('bullet_insert');

	bulletButton.addEventListener('click', function () {
		insert('- ', editor);
	});

}

// Adds italics to content.
function addItalics (document, editor) {

	var bulletButton = document.getElementById('italics_insert');

	bulletButton.addEventListener('click', function () {
		insert('**', editor);
		editor.editArea.selectionStart--;
		editor.editArea.selectionEnd--;
	});

}

// Adds bold to content.
function addBold (document, editor) {

	var bulletButton = document.getElementById('bold_insert');

	bulletButton.addEventListener('click', function () {
		insert('****', editor);
		editor.editArea.selectionStart -= 2;
		editor.editArea.selectionEnd -= 2;
	});

}

// Adds link to content.
function addLink (document, editor) {

	var linkButton = document.getElementById('link_insert');

	linkButton.addEventListener('click', function () {
		insert('[text](https://)', editor);
		editor.editArea.selectionStart -= 1;
		editor.editArea.selectionEnd -= 1;
	});

}

// Adds code block to content.
function addCode (document, editor) {

	var linkButton = document.getElementById('code_insert');

	linkButton.addEventListener('click', function () {
		insert('```\n\n```', editor);
		editor.editArea.selectionStart -= 4;
		editor.editArea.selectionEnd -= 4;
	});

}

// Builds the insert toolbar.
function setupToolbar (document, editor) {

	addHeadings(document, editor);
	addBullets(document, editor);
	addItalics(document, editor);
	addBold(document, editor);
	addLink(document, editor);
	addCode(document, editor);

}


// ----- Module Exports ----- //

module.exports = {

	setupToolbar: setupToolbar

};<commit_msg>Refactored code for insert toolbar, removed repeated code.<commit_after>// ----- Setup ----- //

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


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

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

	var editArea = editor.editArea;
	var selectStart = editArea.selectionStart;
	var selectEnd = editArea.selectionEnd;

	editArea.value = editArea.value.substring(0, selectStart) + snippet +
		editArea.value.substring(selectEnd);

	editArea.focus();
	editor.updatePreview();

}

// Adds headings to content, H1 through H6.
function addHeadings (document, editor) {

	var headingButtons = document.getElementsByClassName('heading_button');

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

	for (var i = headingButtons.length - 1; i >= 0; i--) {
		headingButtons[i].addEventListener('click', addHeading);
	}

}

// Sets up a toolbar button, inserts corresponding snippet into the content.
// E.g. the bullet button will insert '- ', a bullet point.
function setupButton (document, editor, buttonName, snippet, caretMove) {

	var button = document.getElementById(buttonName + '_insert');

	button.addEventListener('click', function () {

		insert(snippet, editor);

		if (caretMove) {
			editor.editArea.selectionStart -= caretMove;
			editor.editArea.selectionEnd -= caretMove;
		}

	});

}

// Builds the insert toolbar.
function setupToolbar (document, editor) {

	addHeadings(document, editor);

	setupButton(document, editor, 'bullet', '- ');
	setupButton(document, editor, 'italics', '**', 1);
	setupButton(document, editor, 'bold', '****', 2);
	setupButton(document, editor, 'link', '[text](https://)', 1);
	setupButton(document, editor, 'code', '```\n\n```', 4);

}


// ----- Module Exports ----- //

module.exports = {

	setupToolbar: setupToolbar

};
{'fraction_non_alphanumeric': 0.17132175060479438, 'fraction_numerical': 0.006597756762700682, 'mean_word_length': 4.008810572687224, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2442041', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1411, 'n_words': 333}