small fixes

This commit is contained in:
zadam 2022-12-23 14:18:40 +01:00
parent 3804d2df8c
commit 698eb53006
7 changed files with 35 additions and 28 deletions

View File

@ -5,6 +5,7 @@ const AbstractEntity = require("./abstract_entity");
const sql = require("../../services/sql"); const sql = require("../../services/sql");
const dateUtils = require("../../services/date_utils"); const dateUtils = require("../../services/date_utils");
const promotedAttributeDefinitionParser = require("../../services/promoted_attribute_definition_parser"); const promotedAttributeDefinitionParser = require("../../services/promoted_attribute_definition_parser");
const {sanitizeAttributeName} = require("../../services/sanitize_attribute_name");
/** /**
* Attribute is an abstract concept which has two real uses - label (key - value pair) * Attribute is an abstract concept which has two real uses - label (key - value pair)
@ -178,6 +179,8 @@ class Attribute extends AbstractEntity {
beforeSaving() { beforeSaving() {
this.validate(); this.validate();
this.name = sanitizeAttributeName(this.name);
if (!this.value) { if (!this.value) {
// null value isn't allowed // null value isn't allowed
this.value = ""; this.value = "";

View File

@ -5,6 +5,7 @@ const imageService = require('../../services/image');
const dateNoteService = require('../../services/date_notes'); const dateNoteService = require('../../services/date_notes');
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes'); const attributeService = require('../../services/attributes');
const {sanitizeAttributeName} = require("../../services/sanitize_attribute_name.js");
function uploadImage(req) { function uploadImage(req) {
const file = req.file; const file = req.file;
@ -25,7 +26,7 @@ function uploadImage(req) {
const labels = JSON.parse(labelsStr); const labels = JSON.parse(labelsStr);
for (const {name, value} of labels) { for (const {name, value} of labels) {
note.setLabel(attributeService.sanitizeAttributeName(name), value); note.setLabel(sanitizeAttributeName(name), value);
} }
} }
@ -50,7 +51,7 @@ function saveNote(req) {
if (req.body.labels) { if (req.body.labels) {
for (const {name, value} of req.body.labels) { for (const {name, value} of req.body.labels) {
note.setLabel(attributeService.sanitizeAttributeName(name), value); note.setLabel(sanitizeAttributeName(name), value);
} }
} }

View File

@ -128,20 +128,6 @@ function isAttributeDangerous(type, name) {
); );
} }
function sanitizeAttributeName(origName) {
let fixedName;
if (origName === '') {
fixedName = "unnamed";
}
else {
// any not allowed character should be replaced with underscore
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_");
}
return fixedName;
}
module.exports = { module.exports = {
getNotesWithLabel, getNotesWithLabel,
getNotesWithLabelFast, getNotesWithLabelFast,
@ -151,6 +137,5 @@ module.exports = {
createAttribute, createAttribute,
getAttributeNames, getAttributeNames,
isAttributeType, isAttributeType,
isAttributeDangerous, isAttributeDangerous
sanitizeAttributeName
}; };

View File

@ -9,10 +9,10 @@ const cls = require('./cls');
const entityChangesService = require('./entity_changes'); const entityChangesService = require('./entity_changes');
const optionsService = require('./options'); const optionsService = require('./options');
const Branch = require('../becca/entities/branch'); const Branch = require('../becca/entities/branch');
const attributeService = require('./attributes');
const noteRevisionService = require('./note_revisions'); const noteRevisionService = require('./note_revisions');
const becca = require("../becca/becca"); const becca = require("../becca/becca");
const utils = require("../services/utils"); const utils = require("../services/utils");
const {sanitizeAttributeName} = require("./sanitize_attribute_name.js");
const noteTypes = require("../services/note_types").getNoteTypeNames(); const noteTypes = require("../services/note_types").getNoteTypeNames();
class ConsistencyChecks { class ConsistencyChecks {
@ -607,7 +607,7 @@ class ConsistencyChecks {
const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`); const attrNames = sql.getColumn(`SELECT DISTINCT name FROM attributes`);
for (const origName of attrNames) { for (const origName of attrNames) {
const fixedName = attributeService.sanitizeAttributeName(origName); const fixedName = sanitizeAttributeName(origName);
if (fixedName !== origName) { if (fixedName !== origName) {
if (this.autoFix) { if (this.autoFix) {

View File

@ -9,6 +9,7 @@ const imageService = require("../image");
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const htmlSanitizer = require("../html_sanitizer"); const htmlSanitizer = require("../html_sanitizer");
const attributeService = require("../attributes"); const attributeService = require("../attributes");
const {sanitizeAttributeName} = require("../sanitize_attribute_name.js");
// date format is e.g. 20181121T193703Z // date format is e.g. 20181121T193703Z
function parseDate(text) { function parseDate(text) {
@ -117,7 +118,7 @@ function importEnex(taskContext, file, parentNote) {
labelName = 'sourceUrl'; labelName = 'sourceUrl';
} }
labelName = attributeService.sanitizeAttributeName(labelName); labelName = sanitizeAttributeName(labelName);
note.attributes.push({ note.attributes.push({
type: 'label', type: 'label',
@ -166,7 +167,7 @@ function importEnex(taskContext, file, parentNote) {
} else if (currentTag === 'tag') { } else if (currentTag === 'tag') {
note.attributes.push({ note.attributes.push({
type: 'label', type: 'label',
name: attributeService.sanitizeAttributeName(text), name: sanitizeAttributeName(text),
value: '' value: ''
}) })
} }

View File

@ -0,0 +1,18 @@
function sanitizeAttributeName(origName) {
let fixedName;
if (origName === '') {
fixedName = "unnamed";
}
else {
// any not allowed character should be replaced with underscore
fixedName = origName.replace(/[^\p{L}\p{N}_:]/ug, "_");
}
return fixedName;
}
module.exports = {
sanitizeAttributeName
};

View File

@ -36,14 +36,13 @@ function getInboxNote(date) {
function createSqlConsole() { function createSqlConsole() {
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
parentNoteId: getMonthlyParentNoteId('_sqlConsole'), parentNoteId: getMonthlyParentNoteId('_sqlConsole', 'sqlConsole'),
title: 'SQL Console', title: 'SQL Console - ' + dateUtils.localNowDate(),
content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n", content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
type: 'code', type: 'code',
mime: 'text/x-sqlite;schema=trilium' mime: 'text/x-sqlite;schema=trilium'
}); });
note.setLabel('_sqlConsole', dateUtils.localNowDate());
note.setLabel('iconClass', 'bx bx-data'); note.setLabel('iconClass', 'bx bx-data');
note.setLabel('keepCurrentHoisting'); note.setLabel('keepCurrentHoisting');
@ -71,7 +70,7 @@ function saveSqlConsole(sqlConsoleNoteId) {
function createSearchNote(searchString, ancestorNoteId) { function createSearchNote(searchString, ancestorNoteId) {
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
parentNoteId: getMonthlyParentNoteId('_search'), parentNoteId: getMonthlyParentNoteId('_search', 'search'),
title: `Search: ${searchString}`, title: `Search: ${searchString}`,
content: "", content: "",
type: 'search', type: 'search',
@ -118,9 +117,9 @@ function saveSearchNote(searchNoteId) {
return result; return result;
} }
function getMonthlyParentNoteId(rootNoteId) { function getMonthlyParentNoteId(rootNoteId, prefix) {
const month = dateUtils.localNowDate().substring(0, 7); const month = dateUtils.localNowDate().substring(0, 7);
const labelName = `${rootNoteId}MonthNote`; const labelName = `${prefix}MonthNote`;
let monthNote = searchService.findFirstNoteWithQuery(`#${labelName}="${month}"`, let monthNote = searchService.findFirstNoteWithQuery(`#${labelName}="${month}"`,
new SearchContext({ancestorNoteId: rootNoteId})); new SearchContext({ancestorNoteId: rootNoteId}));