UNPKG

@fontoxml/fontoxml-development-tools

Version:

Development tools for Fonto.

113 lines (104 loc) 2.71 kB
const referencesRepository = [ { permanentId: '680258CB-B299-4955-B8C2-9ED44D11EF95', target: 'http://www.fontoxml.com', title: 'Fonto - Structured content authoring made simple', description: 'Fonto is the user-friendly web based XML editor that helps you to increase your content quality while eliminating costs.', type: 'web', metadata: {}, }, { permanentId: 1, target: 'http://www.google.nl', title: 'Google', description: 'Google is one of the five most popular websites in the world.', type: 'web', metadata: {}, }, { permanentId: 2, target: 'http://www.someinvalidwebsitetotestissuspiciousflag.com', type: 'web', metadata: { isSuspicious: true, suspiciousReason: 'The domain does not exist.', }, }, { permanentId: 3, target: 'topic.dita', type: 'document', metadata: {}, }, ]; export default { create: function create( referenceType, target, title, description, metadata ) { // Check if target is already in repository const references = referencesRepository.filter(function (reference) { return ( reference.target === target && reference.type === referenceType ); }); let reference = references.length ? references[0] : null; if (reference) { // Merge metadata Object.keys(metadata).forEach(function (property) { reference.metadata[property] = metadata[property]; }); } else { reference = { // For the dev server, pack essential info in the permanent id permanentId: `pid:::${referenceType}:::${target}`, target, type: referenceType, title, description, metadata: metadata || {}, }; referencesRepository.push(reference); } return reference; }, get: function get(permanentId) { const references = referencesRepository.filter(function (reference) { return reference.permanentId === permanentId; }); if (references.length) { return references[0]; } // Reconstruct reference from the permanent id in case the dev server was reset if (permanentId && permanentId.indexOf('pid:::') === 0) { const parts = permanentId.split(':::'); return { permanentId, target: parts[2], type: parts[1], title: parts[2], description: '', metadata: {}, }; } return null; }, metadataUpdate: function metadataUpdate(permanentId, metadata) { const references = referencesRepository.filter(function (reference) { return reference.permanentId === permanentId; }); const reference = references.length ? references[0] : null; if (!reference) { return false; } Object.keys(metadata).forEach(function (property) { reference.metadata[property] = metadata[property]; }); return reference; }, };