Add 'announcement' to v2 homepage api

This requires an accompanying commit in the homepages repo (see "Add announcement support").

Using `raw-loader` to import markdown files as a string works for the app-side, but didn't work well in web-side. Falling back to copying the announcement files to `dist` and reading them via `readFileSync`. Don't really like this -- will return with an alternative.
This commit is contained in:
infinite-persistence 2022-05-18 17:06:21 +08:00
parent b5dd24ff56
commit 245eb39892
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
3 changed files with 35 additions and 1 deletions

View file

@ -82,6 +82,18 @@ export const selectHomepageMeme = (state) => {
return homepages ? homepages['en'].meme || {} : {}; return homepages ? homepages['en'].meme || {} : {};
}; };
export const selectHomepageAnnouncement = (state) => {
const homepageCode = selectHomepageCode(state);
const homepages = window.homepages;
if (homepages) {
const news = homepages[homepageCode].announcement;
if (news) {
return news;
}
}
return homepages ? homepages['en'].announcement || '' : '';
};
export const selectInRegionByCode = (state, code) => { export const selectInRegionByCode = (state, code) => {
const hp = selectClientSetting(state, SETTINGS.HOMEPAGE); const hp = selectClientSetting(state, SETTINGS.HOMEPAGE);
const lang = selectLanguage(state); const lang = selectLanguage(state);

View file

@ -1,9 +1,24 @@
const path = require('path');
const memo = {}; const memo = {};
const loadAnnouncements = (homepageKeys) => {
const fs = require('fs');
const announcements = {};
homepageKeys.forEach((key) => {
const file = path.join(__dirname, `../dist/announcement/${key.toLowerCase()}.md`);
const announcement = fs.readFileSync(file, 'utf8');
announcements[key] = announcement ? announcement.trim() : '';
});
return announcements;
};
// this didn't seem to help. // this didn't seem to help.
if (!memo.homepageData) { if (!memo.homepageData) {
try { try {
memo.homepageData = require('../../custom/homepages/v2'); memo.homepageData = require('../../custom/homepages/v2');
memo.announcements = loadAnnouncements(Object.keys(memo.homepageData));
} catch (err) { } catch (err) {
console.log('getHomepageJSON:', err); console.log('getHomepageJSON:', err);
} }
@ -30,7 +45,10 @@ const getHomepageJsonV2 = () => {
const v2 = {}; const v2 = {};
const homepageKeys = Object.keys(memo.homepageData); const homepageKeys = Object.keys(memo.homepageData);
homepageKeys.forEach((hp) => { homepageKeys.forEach((hp) => {
v2[hp] = memo.homepageData[hp]; v2[hp] = {
...memo.homepageData[hp],
announcement: memo.announcements[hp],
};
}); });
return v2; return v2;
}; };

View file

@ -68,6 +68,10 @@ const copyWebpackCommands = [
from: `${WEB_STATIC_ROOT}/pwa/`, from: `${WEB_STATIC_ROOT}/pwa/`,
to: `${DIST_ROOT}/public/pwa/`, to: `${DIST_ROOT}/public/pwa/`,
}, },
{
from: `${STATIC_ROOT}/../custom/homepages/v2/announcement`,
to: `${DIST_ROOT}/announcement`,
},
]; ];
const CUSTOM_OG_PATH = `${CUSTOM_ROOT}/v2-og.png`; const CUSTOM_OG_PATH = `${CUSTOM_ROOT}/v2-og.png`;