Files
insertr/lib/src/core/insertr.js

45 lines
1.3 KiB
JavaScript

/**
* InsertrCore - Core functionality for content management
*/
export class InsertrCore {
constructor(options = {}) {
this.options = {
apiEndpoint: options.apiEndpoint || '/api/content',
siteId: options.siteId || 'default',
...options
};
}
// Find all enhanced elements on the page
findEnhancedElements() {
return document.querySelectorAll('.insertr');
}
// Get element metadata
getElementMetadata(element) {
const existingId = element.getAttribute('data-content-id');
return {
contentId: existingId,
element: element,
htmlMarkup: element.outerHTML // Server will generate ID from this
};
}
// Get current file path from URL for consistent ID generation
getCurrentFilePath() {
const path = window.location.pathname;
if (path === '/' || path === '') {
return 'index.html';
}
// Remove leading slash: "/about.html" → "about.html"
return path.replace(/^\//, '');
}
// Get all elements with their metadata
getAllElements() {
const elements = this.findEnhancedElements();
return Array.from(elements).map(el => this.getElementMetadata(el));
}
}