2020-07-17 06:08:28 +08:00
|
|
|
import ws from "./ws.js";
|
2020-08-27 20:54:56 +08:00
|
|
|
import linkService from "./link.js";
|
2020-07-17 06:08:28 +08:00
|
|
|
|
|
|
|
function renderAttribute(attribute, $container, renderIsInheritable) {
|
|
|
|
const isInheritable = renderIsInheritable && attribute.isInheritable ? `(inheritable)` : '';
|
|
|
|
|
|
|
|
if (attribute.type === 'label') {
|
|
|
|
$container.append(document.createTextNode('#' + attribute.name + isInheritable));
|
|
|
|
|
|
|
|
if (attribute.value) {
|
|
|
|
$container.append('=');
|
|
|
|
$container.append(document.createTextNode(formatValue(attribute.value)));
|
|
|
|
}
|
|
|
|
|
2020-09-03 05:16:56 +08:00
|
|
|
$container.append(" ");
|
2020-07-17 06:08:28 +08:00
|
|
|
} else if (attribute.type === 'relation') {
|
|
|
|
if (attribute.isAutoLink) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attribute.value) {
|
|
|
|
$container.append(document.createTextNode('~' + attribute.name + isInheritable + "="));
|
|
|
|
$container.append(createNoteLink(attribute.value));
|
2020-09-03 05:16:56 +08:00
|
|
|
$container.append(" ");
|
2020-07-17 06:08:28 +08:00
|
|
|
} else {
|
|
|
|
ws.logError(`Relation ${attribute.attributeId} has empty target`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ws.logError("Unknown attr type: " + attribute.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatValue(val) {
|
|
|
|
if (/^[\p{L}\p{N}\-_,.]+$/u.test(val)) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
else if (!val.includes('"')) {
|
|
|
|
return '"' + val + '"';
|
|
|
|
}
|
|
|
|
else if (!val.includes("'")) {
|
|
|
|
return "'" + val + "'";
|
|
|
|
}
|
|
|
|
else if (!val.includes("`")) {
|
|
|
|
return "`" + val + "`";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return '"' + val.replace(/"/g, '\\"') + '"';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNoteLink(noteId) {
|
2020-08-27 20:54:56 +08:00
|
|
|
const $link = $("<a>", {
|
2020-07-17 06:08:28 +08:00
|
|
|
href: '#' + noteId,
|
|
|
|
class: 'reference-link',
|
|
|
|
'data-note-path': noteId
|
|
|
|
});
|
2020-08-27 20:54:56 +08:00
|
|
|
|
|
|
|
linkService.loadReferenceLinkTitle(noteId, $link);
|
|
|
|
|
|
|
|
return $link;
|
2020-07-17 06:08:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
renderAttribute
|
|
|
|
}
|