Skip to content

Commit

Permalink
test templates
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyundai committed Mar 28, 2024
1 parent 7dc0a38 commit 26baaf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 45 deletions.
68 changes: 23 additions & 45 deletions blocks/page-server/page-server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { getMetadata } from '../../utils/utils.js';
import fetchPageData, { flattenObject } from '../../utils/event-apis.js';
import { getLibs } from '../../scripts/utils.js';

const PLACEHOLDER_REG = /\[\[(.*?)\]\]/g;
const preserveFormatKeys = [
'event-description',
];

const REG = /\[\[(.*?)\]\]/g;

function handleRegisterButton(a) {
const signIn = () => {
Expand Down Expand Up @@ -50,7 +52,7 @@ function autoUpdateLinks(scope) {
function updateImgTag(child, matchCallback, parentElement) {
const parentPic = child.closest('picture');
const originalAlt = child.alt;
const replacedSrc = originalAlt.replace(PLACEHOLDER_REG, matchCallback);
const replacedSrc = originalAlt.replace(REG, (_match, p1) => matchCallback(_match, p1, child));

if (replacedSrc && parentPic && replacedSrc !== originalAlt) {
parentPic.querySelectorAll('source').forEach((el) => {
Expand All @@ -68,76 +70,52 @@ function updateImgTag(child, matchCallback, parentElement) {
window.lana?.log(`failed to convert optimized img from ${el} with dynamic data: ${e}`);
}
});
} else if (originalAlt.match(PLACEHOLDER_REG)) {
} else if (originalAlt.match(REG)) {
parentElement.remove();
}
}

function updateTextNode(child, matchCallback) {
const originalText = child.nodeValue;
const replacedText = originalText.replace(PLACEHOLDER_REG, matchCallback);
const replacedText = originalText.replace(REG, matchCallback);
if (replacedText !== originalText) child.nodeValue = replacedText;
}

function autoUpdateMetadata(res) {
if (!res) return;

if (res['contentArea.title']) document.title = res['contentArea.title'];

if (!res['contentArea.description']) return;

const metaDescription = document.querySelector("meta[name='description']");
if (metaDescription) {
metaDescription.setAttribute('content', res['contentArea.description']);
} else {
const newMetaDescription = document.createElement('meta');
newMetaDescription.setAttribute('name', 'description');
newMetaDescription.setAttribute('content', res['contentArea.description']);
document.head.appendChild(newMetaDescription);
}
}

// data -> dom gills
export async function autoUpdateContent(parent, data, isStructured = false) {
export async function autoUpdateContent(parent) {
if (!parent) {
window.lana?.log('page server block cannot find its parent element');
return null;
return;
}

if (!data) {
document.body.style.display = 'none';
window.location.replace('/404');
}
const getContent = (_match, p1, n) => {
const content = getMetadata(p1) || '';
if (preserveFormatKeys.includes(p1)) {
n.parentNode?.classList.add('preserve-format');
}
return content;
};

const res = isStructured ? flattenObject(data) : data;
console.log('Replacing content with:', res);
const findRegexMatch = (_match, p1) => res[p1] || '';
const allElements = parent.querySelectorAll('*');

allElements.forEach((element) => {
if (element.childNodes.length) {
element.childNodes.forEach((child) => {
if (child.tagName === 'IMG' && child.nodeType === 1) {
updateImgTag(child, findRegexMatch, element);
element.childNodes.forEach((n) => {
if (n.tagName === 'IMG' && n.nodeType === 1) {
updateImgTag(n, getContent, element);
}

if (child.nodeType === 3) {
updateTextNode(child, findRegexMatch);
if (n.nodeType === 3) {
updateTextNode(n, getContent);
}
});
}
});

// handle link replacement. To keep when switching to metadata based rendering
autoUpdateLinks(parent);

// TODO: handle Metadata
autoUpdateMetadata(res);
return res;
}

export default async function init(el) {
const { default: getUuid } = await import(`${getLibs()}/utils/getUuid.js`);
const hash = await getUuid(window.location.pathname);
await autoUpdateContent(el.closest('main'), await fetchPageData(hash, true), true);
await autoUpdateContent(el.closest('main'));
}
5 changes: 5 additions & 0 deletions fstab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ mountpoints:

folders:
/t3/event: /t3/event/default

folders:
/event/t3:
resource: /event/t3/event-detail-template
metadata: /event/t3/event-detail-metadata.json

0 comments on commit 26baaf4

Please sign in to comment.