Skip to content

Commit

Permalink
Выносит функцию в отдельный модуль (#1281)
Browse files Browse the repository at this point in the history
* Выносит функцию в отдельный модуль

* Решает с подключением модуля

* Поднимает версию web-features до 1.0.0 (#1279)

Исправляет импорт из модуля `web-features`.
FYI: Начиная с версии 0.10 требуется использовать именованый импорт.
см: https://www.npmjs.com/package/web-features/v/0.10.0
https://github.com/web-platform-dx/web-features/releases/tag/v0.10.0

* Подстраивается под особенность слонёнка Дампо не собираться с мыслями в 11ty

---------

Co-authored-by: Vitya <[email protected]>
  • Loading branch information
igsekor and vitya-ne authored Aug 26, 2024
1 parent 5023979 commit 682fa2a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 43 deletions.
15 changes: 15 additions & 0 deletions src/scripts/modules/transform-article-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = function transformArticleData(article) {
const section = article.filePathStem.split('/')[1]

return {
title: article.data.title,
cover: article.data.cover ?? {},
get imageLink() {
return `${this.cover.mobile}`
},
description: article.data.description,
link: `/${section}/${article.fileSlug}/`,
linkTitle: article.data.title.replace(/`/g, ''),
section,
}
}
13 changes: 5 additions & 8 deletions src/views/doc-json.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ function hasTag(tags, tag) {
return (tags || []).includes(tag)
}

// TODO: вынести эту функцию в отдельный файл и переиспользовать в `views.11tydata.js`
function transformArticleData(article) {
const section = article.filePathStem.split('/')[1]

return `/${section}/${article.fileSlug}/`
function getArticlePath(article) {
return `/${article.filePathStem.split('/')[1]}/${article.fileSlug}/`
}

function getRandomString(length) {
Expand Down Expand Up @@ -125,7 +122,7 @@ module.exports = {

const articleId = linkedArticles?.[docId]?.next?.id
const articleData = docsById[articleId]
return articleData && transformArticleData(articleData)
return articleData && getArticlePath(articleData)
},

previousArticle: function (data) {
Expand All @@ -135,7 +132,7 @@ module.exports = {

const articleId = linkedArticles?.[docId]?.previous?.id
const articleData = docsById[articleId]
return articleData && transformArticleData(articleData)
return articleData && getArticlePath(articleData)
},

relatedArticles: function (data) {
Expand All @@ -147,7 +144,7 @@ module.exports = {
?.slice(0, 3)
?.map((articleId) => docsById[articleId])
?.filter(Boolean)
?.map((articleData) => transformArticleData(articleData))
?.map((articleData) => getArticlePath(articleData))
},

linksAndImages: async function (data) {
Expand Down
24 changes: 4 additions & 20 deletions src/views/doc.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs')
const { baseUrl } = require('../../config/constants')
const { titleFormatter } = require('../libs/title-formatter/title-formatter')
const roleCollection = require('../libs/role-constructor/collection.json')
const transformArticleData = require('../scripts/modules/transform-article-data.js')

function getPersons(personGetter) {
return function (data) {
Expand All @@ -24,7 +25,7 @@ function getPopulatedPersons(personKey) {
data: {
name: personId,
},
}
},
)
}
}
Expand All @@ -40,23 +41,6 @@ function assignGreaterValue(map, item, key) {
return map
}

// TODO: вынести эту функцию в отдельный файл и переиспользовать в `views.11tydata.js`
function transformArticleData(article) {
const section = article.filePathStem.split('/')[1]

return {
title: article.data.title,
cover: article.data.cover,
get imageLink() {
return `${this.cover.mobile}`
},
description: article.data.description,
link: `/${section}/${article.fileSlug}/`,
linkTitle: article.data.title.replace(/`/g, ''),
section,
}
}

const asyncFilter = async (arr, predicate) => {
const results = await Promise.all(arr.map(predicate))
return arr.filter((_v, index) => results[index])
Expand Down Expand Up @@ -289,7 +273,7 @@ module.exports = {
}
return map
},
{ chrome: 0, edge: 0, firefox: 0, safari: 0 }
{ chrome: 0, edge: 0, firefox: 0, safari: 0 },
)
const supported = doc.data.baseline
.filter((g) => webFeatures[g.group].is_baseline)
Expand All @@ -302,7 +286,7 @@ module.exports = {
}
return map
},
{ chrome: true, edge: true, firefox: true, safari: true }
{ chrome: true, edge: true, firefox: true, safari: true },
)
const flagged = { chrome: false, edge: false, firefox: false, safari: false }
const preview = { chrome: false, edge: false, firefox: false, safari: false }
Expand Down
17 changes: 2 additions & 15 deletions src/views/views.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const categoryColors = require('../../config/category-colors')
const { titleFormatter } = require('../libs/title-formatter/title-formatter')
const { setPath } = require('../libs/collection-helpers/set-path')
const { isProdEnv } = require('../../config/env.js')
const transformArticleData = require('../scripts/modules/transform-article-data.js')

function isExternalURL(url) {
return url.startsWith('http://') || url.startsWith('https://') || url.startsWith('//')
Expand Down Expand Up @@ -96,21 +97,7 @@ module.exports = {
.slice(0, featuredArticlesMaxCount)
.map((id) => docsById[id])
.filter(Boolean)
.map((article) => {
const section = article.filePathStem.split('/')[1]

return {
title: article?.data?.title,
cover: article?.data?.cover,
get imageLink() {
return `${this.cover?.mobile}`
},
description: article?.data?.description,
link: `/${section}/${article?.fileSlug}/`,
linkTitle: article?.data?.title.replace(/`/g, ''),
section,
}
})
.map((article) => transformArticleData(article))
},

themeColor: function (data) {
Expand Down

0 comments on commit 682fa2a

Please sign in to comment.