mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +01:00 
			
		
		
		
	feat(code_notes): reintroduce ESLint for server notes
This commit is contained in:
		
							
								
								
									
										100
									
								
								libraries/codemirror/eslint.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								libraries/codemirror/eslint.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,100 @@ | |||||||
|  | // CodeMirror, copyright (c) by Marijn Haverbeke and others | ||||||
|  | // Distributed under an MIT license: http://codemirror.net/LICENSE | ||||||
|  |  | ||||||
|  | (function(mod) { | ||||||
|  |   if (typeof exports == "object" && typeof module == "object") // CommonJS | ||||||
|  |     mod(require("../../lib/codemirror")); | ||||||
|  |   else if (typeof define == "function" && define.amd) // AMD | ||||||
|  |     define(["../../lib/codemirror"], mod); | ||||||
|  |   else // Plain browser env | ||||||
|  |     mod(CodeMirror); | ||||||
|  | })(function(CodeMirror) { | ||||||
|  |     "use strict"; | ||||||
|  |  | ||||||
|  |     async function validatorHtml(text, options) { | ||||||
|  |         const result = /<script[^>]*>([\s\S]+)<\/script>/ig.exec(text); | ||||||
|  |  | ||||||
|  |         if (result !== null) { | ||||||
|  |             // preceding code is copied over but any (non-newline) character is replaced with space | ||||||
|  |             // this will preserve line numbers etc. | ||||||
|  |             const prefix = text.substr(0, result.index).replace(/./g, " "); | ||||||
|  |  | ||||||
|  |             const js = prefix + result[1]; | ||||||
|  |  | ||||||
|  |             return await validatorJavaScript(js, options); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return []; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async function validatorJavaScript(text, options) { | ||||||
|  |         if (glob.isMobile() | ||||||
|  |             || glob.getActiveContextNote() == null | ||||||
|  |             || glob.getActiveContextNote().mime === 'application/json') { | ||||||
|  |             // eslint doesn't seem to validate pure JSON well | ||||||
|  |             return []; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         await glob.requireLibrary(glob.ESLINT); | ||||||
|  |  | ||||||
|  |         if (text.length > 20000) { | ||||||
|  |             console.log("Skipping linting because of large size: ", text.length); | ||||||
|  |  | ||||||
|  |             return []; | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const errors = new eslint().verify(text, { | ||||||
|  |             root: true, | ||||||
|  |             parserOptions: { | ||||||
|  |                 ecmaVersion: "latest" | ||||||
|  |             }, | ||||||
|  |             extends: ['eslint:recommended', 'airbnb-base'], | ||||||
|  |             env: { | ||||||
|  |                 'browser': true, | ||||||
|  |                 'node': true | ||||||
|  |             }, | ||||||
|  |             rules: { | ||||||
|  |                 'import/no-unresolved': 'off', | ||||||
|  |                 'func-names': 'off', | ||||||
|  |                 'comma-dangle': ['warn'], | ||||||
|  |                 'padded-blocks': 'off', | ||||||
|  |                 'linebreak-style': 'off', | ||||||
|  |                 'class-methods-use-this': 'off', | ||||||
|  |                 'no-unused-vars': ['warn', { vars: 'local', args: 'after-used' }], | ||||||
|  |                 'no-nested-ternary': 'off', | ||||||
|  |                 'no-underscore-dangle': ['error', {'allow': ['_super', '_lookupFactory']}] | ||||||
|  |             }, | ||||||
|  |             globals: { | ||||||
|  |                 "api": "readonly" | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         console.log(errors); | ||||||
|  |  | ||||||
|  |         const result = []; | ||||||
|  |         if (errors) { | ||||||
|  |             parseErrors(errors, result); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return result; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     CodeMirror.registerHelper("lint", "javascript", validatorJavaScript); | ||||||
|  |     CodeMirror.registerHelper("lint", "html", validatorHtml); | ||||||
|  |  | ||||||
|  |     function parseErrors(errors, output) { | ||||||
|  |         for (const error of errors) { | ||||||
|  |             const startLine = error.line - 1; | ||||||
|  |             const endLine = error.endLine !== undefined ? error.endLine - 1 : startLine; | ||||||
|  |             const startCol = error.column - 1; | ||||||
|  |             const endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1; | ||||||
|  |  | ||||||
|  |             output.push({ | ||||||
|  |                 message: error.message, | ||||||
|  |                 severity: error.severity === 1 ? "warning" : "error", | ||||||
|  |                 from: CodeMirror.Pos(startLine, startCol), | ||||||
|  |                 to: CodeMirror.Pos(endLine, endCol) | ||||||
|  |             }); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }); | ||||||
							
								
								
									
										112883
									
								
								libraries/eslint/eslint.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112883
									
								
								libraries/eslint/eslint.js
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| @@ -85,7 +85,6 @@ | |||||||
|     "electron-squirrel-startup": "1.0.1", |     "electron-squirrel-startup": "1.0.1", | ||||||
|     "electron-window-state": "5.0.3", |     "electron-window-state": "5.0.3", | ||||||
|     "escape-html": "1.0.3", |     "escape-html": "1.0.3", | ||||||
|     "eslint": "9.17.0", |  | ||||||
|     "express": "4.21.2", |     "express": "4.21.2", | ||||||
|     "express-rate-limit": "7.5.0", |     "express-rate-limit": "7.5.0", | ||||||
|     "express-session": "1.18.1", |     "express-session": "1.18.1", | ||||||
|   | |||||||
| @@ -26,7 +26,8 @@ const CODE_MIRROR: Library = { | |||||||
|             "node_modules/codemirror/addon/mode/simple.js", |             "node_modules/codemirror/addon/mode/simple.js", | ||||||
|             "node_modules/codemirror/addon/search/match-highlighter.js", |             "node_modules/codemirror/addon/search/match-highlighter.js", | ||||||
|             "node_modules/codemirror/mode/meta.js", |             "node_modules/codemirror/mode/meta.js", | ||||||
|             "node_modules/codemirror/keymap/vim.js" |             "node_modules/codemirror/keymap/vim.js", | ||||||
|  |             "libraries/codemirror/eslint.js" | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         const mimeTypes = mimeTypesService.getMimeTypes(); |         const mimeTypes = mimeTypesService.getMimeTypes(); | ||||||
| @@ -42,7 +43,7 @@ const CODE_MIRROR: Library = { | |||||||
| }; | }; | ||||||
|  |  | ||||||
| const ESLINT: Library = { | const ESLINT: Library = { | ||||||
|     js: ["node_modules/eslint/bin/eslint.js"] |     js: ["libraries/eslint/eslint.js"] | ||||||
| }; | }; | ||||||
|  |  | ||||||
| const RELATION_MAP: Library = { | const RELATION_MAP: Library = { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user