2 var md = window.markdownit({
6 highlight: function (str, lang) {
7 if (lang && hljs.getLanguage(lang)) {
9 return '<pre class="hljs"><code>' +
10 hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
15 return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
19 function scrollToLint(lintId) {
20 var target = document.getElementById(lintId);
24 target.scrollIntoView();
27 function scrollToLintByURL($scope) {
28 var removeListener = $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
29 scrollToLint(window.location.hash.slice(1));
34 function selectGroup($scope, selectedGroup) {
35 var groups = $scope.groups;
36 for (var group in groups) {
37 if (groups.hasOwnProperty(group)) {
38 if (group === selectedGroup) {
41 groups[group] = false;
47 angular.module("clippy", [])
48 .filter('markdown', function ($sce) {
49 return function (text) {
50 return $sce.trustAsHtml(
52 // Oh deer, what a hack :O
53 .replace('<table', '<table class="table"')
57 .directive('themeDropdown', function ($document) {
60 link: function ($scope, $element, $attr) {
61 $element.bind('click', function () {
62 $element.toggleClass('open');
63 $element.addClass('open-recent');
66 $document.bind('click', function () {
67 if (!$element.hasClass('open-recent')) {
68 $element.removeClass('open');
70 $element.removeClass('open-recent');
75 .directive('filterDropdown', function ($document) {
78 link: function ($scope, $element, $attr) {
79 $element.bind('click', function (event) {
80 if (event.target.closest('button')) {
81 $element.toggleClass('open');
83 $element.addClass('open');
85 $element.addClass('open-recent');
88 $document.bind('click', function () {
89 if (!$element.hasClass('open-recent')) {
90 $element.removeClass('open');
92 $element.removeClass('open-recent');
97 .directive('onFinishRender', function ($timeout) {
100 link: function (scope, element, attr) {
101 if (scope.$last === true) {
102 $timeout(function () {
103 scope.$emit(attr.onFinishRender);
109 .controller("lintList", function ($scope, $http, $timeout) {
111 var LEVEL_FILTERS_DEFAULT = {allow: true, warn: true, deny: true, none: true};
112 $scope.levels = LEVEL_FILTERS_DEFAULT;
113 $scope.byLevels = function (lint) {
114 return $scope.levels[lint.level];
117 const GROUPS_FILTER_DEFAULT = {
131 ...GROUPS_FILTER_DEFAULT
134 const THEMES_DEFAULT = {
141 $scope.themes = THEMES_DEFAULT;
143 $scope.versionFilters = {
144 "≥": {enabled: false, minorVersion: null },
145 "≤": {enabled: false, minorVersion: null },
146 "=": {enabled: false, minorVersion: null },
149 $scope.selectTheme = function (theme) {
150 setTheme(theme, true);
153 $scope.toggleLevels = function (value) {
154 const levels = $scope.levels;
155 for (const key in levels) {
156 if (levels.hasOwnProperty(key)) {
162 $scope.toggleGroups = function (value) {
163 const groups = $scope.groups;
164 for (const key in groups) {
165 if (groups.hasOwnProperty(key)) {
171 $scope.resetGroupsToDefault = function () {
172 const groups = $scope.groups;
173 for (const [key, value] of Object.entries(GROUPS_FILTER_DEFAULT)) {
178 $scope.selectedValuesCount = function (obj) {
179 return Object.values(obj).filter(x => x).length;
182 $scope.clearVersionFilters = function () {
183 for (let filter in $scope.versionFilters) {
184 $scope.versionFilters[filter] = { enabled: false, minorVersion: null };
188 $scope.versionFilterCount = function(obj) {
189 return Object.values(obj).filter(x => x.enabled).length;
192 $scope.updateVersionFilters = function() {
193 for (const filter in $scope.versionFilters) {
194 let minorVersion = $scope.versionFilters[filter].minorVersion;
196 // 1.29.0 and greater
197 if (minorVersion && minorVersion > 28) {
198 $scope.versionFilters[filter].enabled = true;
202 $scope.versionFilters[filter].enabled = false;
206 $scope.byVersion = function(lint) {
207 let filters = $scope.versionFilters;
208 for (const filter in filters) {
209 if (filters[filter].enabled) {
210 let minorVersion = filters[filter].minorVersion;
212 // Strip the "pre " prefix for pre 1.29.0 lints
213 let lintVersion = lint.version.startsWith("pre ") ? lint.version.substring(4, lint.version.length) : lint.version;
214 let lintMinorVersion = lintVersion.substring(2, 4);
217 // "=" gets the highest priority, since all filters are inclusive
219 return (lintMinorVersion == minorVersion);
221 if (lintMinorVersion < minorVersion) { return false; }
224 if (lintMinorVersion > minorVersion) { return false; }
235 $scope.byGroups = function (lint) {
236 return $scope.groups[lint.group];
239 $scope.bySearch = function (lint, index, array) {
240 let searchStr = $scope.search;
241 // It can be `null` I haven't missed this value
242 if (searchStr == null || searchStr.length < 3) {
245 searchStr = searchStr.toLowerCase();
246 if (searchStr.startsWith("clippy::")) {
247 searchStr = searchStr.slice(8);
251 if (lint.id.indexOf(searchStr.replace("-", "_")) !== -1) {
255 // Search the description
256 // The use of `for`-loops instead of `foreach` enables us to return early
257 let terms = searchStr.split(" ");
258 let docsLowerCase = lint.docs.toLowerCase();
259 for (index = 0; index < terms.length; index++) {
260 // This is more likely and will therefor be checked first
261 if (docsLowerCase.indexOf(terms[index]) !== -1) {
265 if (lint.id.indexOf(terms[index]) !== -1) {
275 $scope.copyToClipboard = function (lint) {
276 const clipboard = document.getElementById("clipboard-" + lint.id);
278 let resetClipboardTimeout = null;
279 let resetClipboardIcon = clipboard.innerHTML;
281 function resetClipboard() {
282 resetClipboardTimeout = null;
283 clipboard.innerHTML = resetClipboardIcon;
286 navigator.clipboard.writeText("clippy::" + lint.id);
288 clipboard.innerHTML = "✓";
289 if (resetClipboardTimeout !== null) {
290 clearTimeout(resetClipboardTimeout);
292 resetClipboardTimeout = setTimeout(resetClipboard, 1000);
298 $scope.loading = true;
299 // This will be used to jump into the source code of the version that this documentation is for.
300 $scope.docVersion = window.location.pathname.split('/')[2] || "master";
302 if (window.location.hash.length > 1) {
303 $scope.search = window.location.hash.slice(1);
304 $scope.open[window.location.hash.slice(1)] = true;
305 scrollToLintByURL($scope);
308 $http.get('./lints.json')
309 .success(function (data) {
311 $scope.loading = false;
313 var selectedGroup = getQueryVariable("sel");
315 selectGroup($scope, selectedGroup.toLowerCase());
318 scrollToLintByURL($scope);
320 setTimeout(function () {
321 var el = document.getElementById('filter-input');
322 if (el) { el.focus() }
325 .error(function (data) {
327 $scope.loading = false;
330 window.addEventListener('hashchange', function () {
332 $timeout(function () {
333 $scope.levels = LEVEL_FILTERS_DEFAULT;
334 $scope.search = window.location.hash.slice(1);
335 $scope.open[window.location.hash.slice(1)] = true;
337 scrollToLintByURL($scope);
344 function getQueryVariable(variable) {
345 var query = window.location.search.substring(1);
346 var vars = query.split('&');
347 for (var i = 0; i < vars.length; i++) {
348 var pair = vars[i].split('=');
349 if (decodeURIComponent(pair[0]) == variable) {
350 return decodeURIComponent(pair[1]);
355 function setTheme(theme, store) {
356 let enableHighlight = false;
357 let enableNight = false;
358 let enableAyu = false;
369 enableHighlight = true;
372 enableHighlight = true;
377 document.getElementsByTagName("body")[0].className = theme;
379 document.getElementById("githubLightHighlight").disabled = enableNight || !enableHighlight;
380 document.getElementById("githubDarkHighlight").disabled = !enableNight && !enableAyu;
382 document.getElementById("styleHighlight").disabled = !enableHighlight;
383 document.getElementById("styleNight").disabled = !enableNight;
384 document.getElementById("styleAyu").disabled = !enableAyu;
388 localStorage.setItem('clippy-lint-list-theme', theme);
393 // loading the theme after the initial load
394 const prefersDark = window.matchMedia("(prefers-color-scheme: dark)");
395 const theme = localStorage.getItem('clippy-lint-list-theme');
396 if (prefersDark.matches && !theme) {
397 setTheme("coal", false);
399 setTheme(theme, false);