Skip to content

Commit

Permalink
feat: add support for mainTags item in contexts (#1329) (#8693rd6nq)
Browse files Browse the repository at this point in the history
  • Loading branch information
dziudek committed Feb 6, 2024
1 parent feb7840 commit c1969f1
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/back-end/builddata.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "0.44.4",
"build": 16574
"build": 16575
}
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/404.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class RendererContext404 extends RendererContext {
}

this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -65,6 +66,7 @@ class RendererContext404 extends RendererContext {
featuredPosts: this.featuredPosts,
hiddenPosts: this.hiddenPosts,
tags: this.tags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class RendererContextAuthor extends RendererContext {
}

this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -151,6 +152,7 @@ class RendererContextAuthor extends RendererContext {
featuredPosts: this.featuredPosts,
hiddenPosts: this.hiddenPosts,
tags: this.tags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class RendererContextHome extends RendererContext {
this.metaDescription = this.siteConfig.advanced.metaDescription.replace(/%sitename/g, siteName);

this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -102,6 +103,7 @@ class RendererContextHome extends RendererContext {
featuredPosts: this.featuredPosts,
hiddenPosts: this.hiddenPosts,
tags: this.tags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RendererContextPost extends RendererContext {
let metaDataQuery = this.db.prepare(`SELECT value FROM posts_additional_data WHERE post_id = @postID AND key = '_core'`);
this.metaData = metaDataQuery.get({ postID: this.postID});
this.allTags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -407,6 +408,7 @@ class RendererContextPost extends RendererContext {
hiddenPosts: this.hiddenPosts,
relatedPosts: this.relatedPosts,
tags: this.allTags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RendererContextSearch extends RendererContext {
}

this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -65,6 +66,7 @@ class RendererContextSearch extends RendererContext {
featuredPosts: this.featuredPosts,
hiddenPosts: this.hiddenPosts,
tags: this.tags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class RendererContextTag extends RendererContext {
}

this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -153,6 +154,7 @@ class RendererContextTag extends RendererContext {
featuredPosts: this.featuredPosts,
hiddenPosts: this.hiddenPosts,
tags: this.tags,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
2 changes: 2 additions & 0 deletions app/back-end/modules/render-html/contexts/tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const RendererContext = require('../renderer-context.js');
class RendererContextTags extends RendererContext {
loadData() {
this.tags = this.renderer.commonData.tags.filter(tag => tag.additionalData.isHidden !== true);
this.mainTags = this.renderer.commonData.mainTags.filter(maintag => maintag.additionalData.isHidden !== true);
this.menus = this.renderer.commonData.menus;
this.unassignedMenus = this.renderer.commonData.unassignedMenus;
this.authors = this.renderer.commonData.authors;
Expand Down Expand Up @@ -57,6 +58,7 @@ class RendererContextTags extends RendererContext {
hiddenPosts: this.hiddenPosts,
tags: this.tags,
tagsNumber: this.tags.length,
mainTags: this.mainTags,
authors: this.authors,
metaTitleRaw: this.metaTitle,
metaDescriptionRaw: this.metaDescription,
Expand Down
7 changes: 6 additions & 1 deletion app/back-end/modules/render-html/items/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class TagItem {
* @param tagData
* @param rendererInstance
*/
constructor(tag, rendererInstance) {
constructor(tag, rendererInstance, mainTagIDs = []) {
this.tag = tag;
this.tagID = parseInt(tag.id, 10);
this.renderer = rendererInstance;
this.db = this.renderer.db;
this.themeConfig = this.renderer.themeConfig;
this.tagData = {};
this.mainTagIDs = mainTagIDs;

this.prepareData();
this.storeData();
Expand Down Expand Up @@ -61,6 +62,10 @@ class TagItem {

// Store tag data without references
this.renderer.cachedItems.tags[this.tagID] = JSON.parse(JSON.stringify(this.tagData));

if (this.mainTagIDs.indexOf(this.tagID) > -1) {
this.renderer.cachedItems.mainTags[this.tagID] = JSON.parse(JSON.stringify(this.tagData));
}
}

/**
Expand Down
17 changes: 16 additions & 1 deletion app/back-end/modules/render-html/renderer-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ class RendererCache {
* Retrieves tags data
*/
getTags() {
console.time('MAINTAGS - QUERY');
let mainTagIDs = this.db.prepare(`
SELECT DISTINCT
json_extract(value, '$.mainTag') AS mainTagID
FROM
posts_additional_data
WHERE
key = '_core' AND
json_extract(value, '$.mainTag') IS NOT NULL AND
json_extract(value, '$.mainTag') != '';
`).all();

mainTagIDs = mainTagIDs.map(mainTag => mainTag.mainTagID);
console.timeEnd('MAINTAGS - QUERY');

console.time('TAGS - QUERY');
let tags = this.db.prepare(`
SELECT
Expand All @@ -62,7 +77,7 @@ class RendererCache {

tags = tags.map(tag => {
let tagViewConfig = this.getViewSettings(tagViewConfigObject, tag);
let newTag = new Tag(tag, this.renderer);
let newTag = new Tag(tag, this.renderer, mainTagIDs);
newTag.setTagViewConfig(tagViewConfig);
return newTag;
});
Expand Down
24 changes: 24 additions & 0 deletions app/back-end/modules/render-html/renderer-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ class RendererContext {
return tags;
}

getAllMainTags() {
// Retrieve post main tags
let mainTags = this.db.prepare(`
SELECT DISTINCT
json_extract(value, '$.mainTag') AS id
FROM
posts_additional_data
WHERE
key = '_core' AND
json_extract(value, '$.mainTag') IS NOT NULL AND
json_extract(value, '$.mainTag') != '';
`).all();

mainTags = mainTags.map(mainTag => JSON.parse(JSON.stringify(this.renderer.cachedItems.tags[mainTag.id])));

if(!this.siteConfig.advanced.displayEmptyTags) {
mainTags = mainTags.filter(mainTag => mainTag.postsNumber > 0);
}

mainTags.sort((mainTagA, mainTagB) => mainTagA.name.localeCompare(mainTagB.name));

return mainTags;
}

getAuthors() {
let authorData = this.db.prepare(`SELECT id FROM authors ORDER BY id ASC;`).all();
let authors = [];
Expand Down
4 changes: 4 additions & 0 deletions app/back-end/modules/render-html/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Renderer {
this.contentStructure = {};
this.commonData = {
tags: [],
mainTags: [],
authors: [],
menus: [],
featuredPosts: {
Expand All @@ -75,6 +76,7 @@ class Renderer {
postTags: {},
posts: {},
tags: {},
mainTags: {},
tagsPostCounts: {},
authors: {},
authorsPostCounts: {},
Expand Down Expand Up @@ -1753,6 +1755,7 @@ class Renderer {
postTags: {},
posts: {},
tags: {},
mainTags: {},
tagsPostCounts: {},
authors: {},
authorsPostCounts: {},
Expand All @@ -1777,6 +1780,7 @@ class Renderer {

this.commonData = {
tags: globalContextGenerator.getAllTags(),
mainTags: globalContextGenerator.getAllMainTags(),
authors: globalContextGenerator.getAuthors(),
menus: menus,
unassignedMenus: unassignedMenus,
Expand Down

0 comments on commit c1969f1

Please sign in to comment.