Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
btopro committed Aug 11, 2023
1 parent 0146de3 commit 34b84e6
Show file tree
Hide file tree
Showing 11 changed files with 948 additions and 643 deletions.
192 changes: 108 additions & 84 deletions api/apps/haxcms/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { parse } from 'node-html-parser';
// vercel to slice our data into views that we can remix at will
export default async function handler(req, res) {
let siteManifest = {};
let items = [];
// use this if POST data is what's being sent
let body = {};
if (req.query.site) {
Expand Down Expand Up @@ -49,96 +48,121 @@ export default async function handler(req, res) {
siteData = null;
}
siteManifest = await resolveSiteData(base, siteData);
items = siteManifest.orderTree(siteManifest.items);
if (body.tag) {
items = items.filter((item) => {
if (item.metadata && item.metadata.tags && item.metadata.tags.includes(body.tag)) {
siteManifest.items = siteManifest.orderTree(siteManifest.items);
// filter by criteria matching one at a time as this is a intersection, not union query
if (body.tags) {
siteManifest.items = siteManifest.items.filter((item) => {
if (item.metadata && item.metadata.tags && item.metadata.tags.includes(body.tags)) {
return true;
}
return false;
});
}
if (body.parent) {
siteManifest.items = siteManifest.items.filter((item) => {
if (item.parent === body.parent) {
return true;
}
return false;
});
}
if (body.title) {
siteManifest.items = siteManifest.items.filter((item) => {
if (item.title.toLowerCase().indexOf(body.title.toLowerCase()) >= 0) {
return true;
}
return false;
});
siteManifest.items = items;
}
// special support for media as we now need to take the items and load the content
if (body.display === "media") {
for (var i in items) {
let content = await siteManifest.getContentById(items[i].id, true);
siteManifest.items[i].media = {};
let doc = parse(`<div id="wrapper">${content}</div>`);
var data = {};
// run through each type of query and get the HTML references
let dataInclude = ['audio', 'selfChecks', 'objectives', 'authorNotes', 'images', 'h5p', 'headings', 'dataTables','specialTags', 'links', 'placeholders', 'siteremotecontent','readTime','video'];
for (let inc of dataInclude) {
switch (inc) {
case 'audio':
data[inc] = doc.querySelectorAll('audio,audio-player');
break;
case 'video':
data[inc] = doc.querySelectorAll('video-player,iframe[src*="youtube.com"],iframe[src*="youtube-nocookie.com"],iframe[src*="vimeo.com"],video[src],video source[src],a11y-media-player');
break;
case 'selfChecks':
data[inc] = doc.querySelectorAll('iframe.entity_iframe:not(.elmsmedia_h5p_content),self-check,multiple-choice');
break;
case 'h5p':
data[inc] = doc.querySelectorAll('iframe.elmsmedia_h5p_content,iframe[src*="h5p/embed"]');
break;
case 'objectives':
data[inc] = doc.querySelectorAll('instruction-card[type="objectives"] li');
break;
case 'authorNotes':
data[inc] = doc.querySelectorAll('page-flag');
break;
case 'images':
data[inc] = doc.querySelectorAll('media-image,img,simple-img');
break;
case 'headings':
data[inc] = doc.querySelectorAll('h1,h2,h3,h4,h5,h6,relative-heading');
break;
case 'dataTables':
data[inc] = doc.querySelectorAll('table');
break;
case 'specialTags':
data[inc] = doc.querySelectorAll('*:not(p,div,h1,h2,h3,h4,h5,h6,table,bold,li,ul,ol,span,a,em,b,i,strike,u,code,pre,img,hr,tr,td,th)').length;
break;
case 'links':
data[inc] = doc.querySelectorAll('a[href^="http://"],a[href^="https://"]');
break;
case 'placeholders':
data[inc] = doc.querySelectorAll('place-holder');
break;
case 'siteremotecontent':
data[inc] = doc.querySelectorAll('site-remote-content');
break;
// variant on links, not included by default as it does more dom parsing then needed by overview stats
case 'linkData':
var extLinks = doc.querySelectorAll('a[href^="http://"],a[href^="https://"]');
data.linkData = {};
for (let el of extLinks) {
// obtain which page this link shows up on
let parent = el.parentNode;
while (parent && !parent.getAttribute('data-jos-item-id')) {
parent = parent.parentNode;
}
let tmpItem = {
linkTitle: el.innerText,
itemId: parent.getAttribute('data-jos-item-id'),
};
if (data.linkData[el.getAttribute('href')]) {
data.linkData[el.getAttribute('href')].push(tmpItem);
}
else {
data.linkData[el.getAttribute('href')] = [tmpItem];
if (body.displayOf === "blocks" || body.displayOf === "full") {
for (var i in siteManifest.items) {
let content = await siteManifest.getContentById(siteManifest.items[i].id, true);
if (body.displayOf === "full") {
// full content, way easier than deep media parsing
// @note this can be a massive response back to the DOM to render if we have a big site
// comment back in if we find a need for this capability
siteManifest.items[i].contents = content;
}
else {
// building a media response
siteManifest.items[i].media = {};
let doc = parse(`<div id="wrapper">${content}</div>`);
var data = {};
// run through each type of query and get the HTML references
let dataInclude = ['audio', 'selfChecks', 'objectives', 'authorNotes', 'images', 'h5p', 'headings', 'dataTables','specialTags', 'links', 'placeholders','readTime','video'];
if (body.blockFilter && dataInclude.includes(body.blockFilter)) {
dataInclude = [body.blockFilter];
}
for (let inc of dataInclude) {
switch (inc) {
case 'audio':
data[inc] = doc.querySelectorAll('audio,audio-player');
break;
case 'video':
data[inc] = doc.querySelectorAll('video-player,iframe[src*="youtube.com"],iframe[src*="youtube-nocookie.com"],iframe[src*="vimeo.com"],video[src],video source[src],a11y-media-player');
break;
case 'selfChecks':
data[inc] = doc.querySelectorAll('iframe.entity_iframe:not(.elmsmedia_h5p_content),self-check,multiple-choice');
break;
case 'h5p':
data[inc] = doc.querySelectorAll('iframe.elmsmedia_h5p_content,iframe[src*="h5p/embed"]');
break;
case 'objectives':
data[inc] = doc.querySelectorAll('instruction-card[type="objectives"] li');
break;
case 'authorNotes':
data[inc] = doc.querySelectorAll('page-flag');
break;
case 'images':
data[inc] = doc.querySelectorAll('media-image,img,simple-img');
break;
case 'headings':
data[inc] = doc.querySelectorAll('h1,h2,h3,h4,h5,h6,relative-heading');
break;
case 'dataTables':
data[inc] = doc.querySelectorAll('table');
break;
case 'specialTags':
data[inc] = doc.querySelectorAll('*:not(p,div,h1,h2,h3,h4,h5,h6,table,bold,li,ul,ol,span,a,em,b,i,strike,u,code,pre,img,hr,tr,td,th)').length;
break;
case 'links':
data[inc] = doc.querySelectorAll('a[href^="http://"],a[href^="https://"]');
break;
case 'placeholders':
data[inc] = doc.querySelectorAll('place-holder');
break;
// variant on links, not included by default as it does more dom parsing then needed by overview stats
case 'linkData':
var extLinks = doc.querySelectorAll('a[href^="http://"],a[href^="https://"]');
data.linkData = {};
for (let el of extLinks) {
// obtain which page this link shows up on
let parent = el.parentNode;
while (parent && !parent.getAttribute('data-jos-item-id')) {
parent = parent.parentNode;
}
let tmpItem = {
linkTitle: el.innerText,
itemId: parent.getAttribute('data-jos-item-id'),
};
if (data.linkData[el.getAttribute('href')]) {
data.linkData[el.getAttribute('href')].push(tmpItem);
}
else {
data.linkData[el.getAttribute('href')] = [tmpItem];
}
}
}
break;

break;
}
}
}
// run through anythign where we found data and build the HTML of JUST that type
for (var j in data) {
siteManifest.items[i].media[j] = '';
for (var k in data[j]) {
siteManifest.items[i].media[j] += data[j][k].outerHTML + "\n";
// run through anythign where we found data and build the HTML of JUST that type
for (var j in data) {
siteManifest.items[i].media[j] = '';
for (var k in data[j]) {
siteManifest.items[i].media[j] += data[j][k].outerHTML + "\n";
}
}
}
}
Expand All @@ -150,7 +174,7 @@ export default async function handler(req, res) {
if (!body.cacheBuster) {
options.cache = 300;
}
res = stdResponse(res, items, options);
res = stdResponse(res, siteManifest.items || [], options);
}
else {
res = invalidRequest(res, 'site location required');
Expand Down
2 changes: 1 addition & 1 deletion elements/haxcms-elements/demo/wc-registry.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SiteActiveTags extends LitElement {
return html`${this.tags && this.tags != ""
? this.tags.split(",").map(
(tag) => html`
<a href="x/views?tag=${tag.trim()}">
<a href="x/views?tags=${tag.trim()}">
<simple-tag
?auto-accent-color="${this.autoAccentColor}"
value="${tag.trim()}"
Expand Down
Loading

1 comment on commit 34b84e6

@vercel
Copy link

@vercel vercel bot commented on 34b84e6 Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.