Echo Writes Code

markdown-view-switcher.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
'use strict';

window.addEventListener('DOMContentLoaded', () => {
	const CHECKBOX_QUERY = '#markdown-view-switcher > input[type="checkbox"]';
	const BLOB_BODY_QUERY = '.blob-body';
	const HIDDEN_CLASS = 'hidden';

	const document = window.document;
	const markdownViewEnabledCheckbox = document.querySelector(CHECKBOX_QUERY);
	const blobBody = document.querySelector(BLOB_BODY_QUERY);

	// If the checkbox doesn't exist, we're not on a page with a rendered view, so we're done
	if (!markdownViewEnabledCheckbox) {
		return;
	}

	markdownViewEnabledCheckbox.addEventListener('change', () => {
		for (const section of blobBody.children) {
			section.classList.toggle(HIDDEN_CLASS);
		}
	});
});