Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
VitalikL committed Jun 15, 2024
1 parent c88a24a commit 509e58b
Show file tree
Hide file tree
Showing 20 changed files with 528 additions and 69 deletions.
5 changes: 4 additions & 1 deletion ops/deploy/deploy-documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
FIREBASE_DATABASE_BLOCKS,
FIREBASE_DATABASE_SEGMENTS
} from "../helpers/constants.js"
import { SEGMENT_DEFAULT_BLOCK_STYLES } from "../helpers/styles.js"


let getDocumentInfoYml = async function (document) {
Expand All @@ -26,6 +27,7 @@ let getDocumentInfoYml = async function (document) {

documentInfo.id = `${documentPathInfo.language}-${documentPathInfo.type}-${documentPathInfo.title}-${DOCUMENT_CONTENT_DIRNAME}-${documentPathInfo.section || SECTION_DEFAULT_NAME}-${documentPathInfo.document}`
documentInfo.resourceId = `${documentPathInfo.language}-${documentPathInfo.type}-${documentPathInfo.title}`
documentInfo.resourceIndex = `${documentPathInfo.language}/${documentPathInfo.type}/${documentPathInfo.title}`
documentInfo.index = `${documentPathInfo.language}/${documentPathInfo.type}/${documentPathInfo.title}/${DOCUMENT_CONTENT_DIRNAME}/${documentPathInfo.section || SECTION_DEFAULT_NAME}/${documentPathInfo.document}`
documentInfo.name = `${documentPathInfo.document}`

Expand All @@ -43,7 +45,7 @@ let getSegmentInfo = async function (segment, processBlocks = false) {

if (!segmentInfo.type) {
if (processBlocks) {
segmentInfo.blocks = await parseSegment(segmentInfoFrontMatter.body, segmentPathInfo, "root")
segmentInfo.blocks = await parseSegment(segmentInfoFrontMatter.body, segmentPathInfo, "root", 1)
}
segmentInfo.type = SEGMENT_TYPES.BLOCK
}
Expand All @@ -52,6 +54,7 @@ let getSegmentInfo = async function (segment, processBlocks = false) {
segmentInfo.resourceId = `${segmentPathInfo.language}-${segmentPathInfo.type}-${segmentPathInfo.title}`
segmentInfo.index = `${segmentPathInfo.language}/${segmentPathInfo.type}/${segmentPathInfo.title}/${DOCUMENT_CONTENT_DIRNAME}/${segmentPathInfo.section || SECTION_DEFAULT_NAME}/${segmentPathInfo.document}/segments/${segmentPathInfo.segment}`
segmentInfo.name = `${segmentPathInfo.segment}`
segmentInfo.defaultStyles = SEGMENT_DEFAULT_BLOCK_STYLES

return segmentInfo
}
Expand Down
129 changes: 120 additions & 9 deletions ops/deploy/deploy-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fdir } from "fdir"
import { database } from "../helpers/firebase.js"
import { getDocumentInfoYml } from "./deploy-documents.js"
import { getLanguageInfo } from "./deploy-languages.js"
import { isMainModule, parseResourcePath } from "../helpers/helpers.js"
import { isMainModule, parseResourcePath, slug } from "../helpers/helpers.js"
import { getLanguages } from "./deploy-languages.js"
import {
SOURCE_DIR,
Expand All @@ -22,8 +22,9 @@ import {
FEED_SCOPES,
FEED_VIEWS,
FEED_DIRECTION,
FIREBASE_DATABASE_RESOURCES, FIREBASE_DATABASE_LANGUAGES
FIREBASE_DATABASE_RESOURCES, FIREBASE_DATABASE_LANGUAGES, API_URL, API_PREFIX
} from "../helpers/constants.js"
import crypto from "crypto"


let getResourceInfo = async function (resource, depth = 0) {
Expand All @@ -47,13 +48,28 @@ let getResourceInfo = async function (resource, depth = 0) {
resourceInfo.subtitle = [languageInfo.kinds[resourceInfo.kind], resourceInfo.subtitle || undefined, ].filter(e => e !== undefined).join(" · ")
}

if (!depth && resourceInfo.referenceResource && typeof resourceInfo.referenceResource !== "string") {
const referenceResourcePath = `${SOURCE_DIR}/${resourceInfo.referenceResource}/${RESOURCE_INFO_FILENAME}`
if (fs.pathExistsSync(referenceResourcePath)) {
resourceInfo.referenceResource = await getResourceInfo(referenceResourcePath, depth++)
}
if (depth && resourceInfo.featuredResources) {
let featuredResources = await Promise.all(resourceInfo.featuredResources.map(async featuredResource => {
const featuredResourcePath = `${SOURCE_DIR}/${featuredResource}/${RESOURCE_INFO_FILENAME}`
if (fs.pathExistsSync(featuredResourcePath)) {
return await getResourceInfo(featuredResourcePath)
}
}))
resourceInfo.feeds = [{
name: "featured",
id: crypto.createHash("sha256").update(
`${resource}-featured`
).digest("hex"),
direction: featuredResources.length > 1 ? FEED_DIRECTION.HORIZONTAL : FEED_DIRECTION.VERTICAL,
scope: FEED_SCOPES.RESOURCE,
view: FEED_VIEWS.TILE,
title: languageInfo.featuredResources.title,
resources: featuredResources
}]
}

delete resourceInfo.featuredResources

// TODO: make seamless for local testing
if (!resourceInfo.covers) {
resourceInfo.covers = {
Expand All @@ -67,7 +83,7 @@ let getResourceInfo = async function (resource, depth = 0) {
const documents = new fdir()
.withBasePath()
.withRelativePaths()
.withMaxDepth(5)
.withMaxDepth(6)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/${RESOURCE_CONTENT_DIRNAME}/**/info.yml`)
.crawl(`${SOURCE_DIR}/`)
.sync();
Expand All @@ -86,6 +102,85 @@ let getResourceInfo = async function (resource, depth = 0) {
resourceInfo.kind = RESOURCE_KIND.EXTERNAL
}

if (languageInfo.features) {
let features = { ...languageInfo.features }
let resourceFeatures = []

for (let key of Object.keys(features)) {
features[key].image = `${API_URL()}${API_PREFIX}${features[key].image}`
}

if (resourceInfo.features && resourceInfo.features.length) {
for (let feature of resourceInfo.features) {
if (feature && features[feature]) {
resourceFeatures.push(features[feature])
}
}
}

let inside_stories = new fdir()
.withRelativePaths()
.withMaxDepth(5)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/${RESOURCE_CONTENT_DIRNAME}/+(0|1|2|3|4|5|6|7|8|9)/inside-story.md`)
.crawl(`${SOURCE_DIR}/`)
.sync()

if (inside_stories.length && features['inside-story']) {
resourceFeatures.push(features['inside-story'])
}

let teacher_comments = new fdir()
.withRelativePaths()
.withMaxDepth(5)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/${RESOURCE_CONTENT_DIRNAME}/+(0|1|2|3|4|5|6|7|8|9)/teacher-comments.md`)
.crawl(`${SOURCE_DIR}/`)
.sync()

if (teacher_comments.length && features['teacher-comments']) {
resourceFeatures.push(features['teacher-comments'])
}

let audio = new fdir()
.withRelativePaths()
.withMaxDepth(5)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/audio.yml`)
.crawl(`${SOURCE_DIR}/`)
.sync()

if (audio.length && features['audio']) {
resourceFeatures.push(features['audio'])
}

let video = new fdir()
.withRelativePaths()
.withMaxDepth(5)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/video.yml`)
.crawl(`${SOURCE_DIR}/`)
.sync()

if (video.length && features['video']) {
resourceFeatures.push(features['video'])
}

let pdf = new fdir()
.withRelativePaths()
.withMaxDepth(5)
.glob(`${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/pdf.yml`)
.crawl(`${SOURCE_DIR}/`)
.sync()

if (pdf.length && features['original_layout']) {
resourceFeatures.push(features['original_layout'])
}

resourceInfo.features = resourceFeatures.filter((thing, index) => {
const _thing = JSON.stringify(thing)
return index === resourceFeatures.findIndex(obj => {
return JSON.stringify(obj) === _thing
})
})
}

return resourceInfo
}

Expand Down Expand Up @@ -117,13 +212,18 @@ let processResources = async function (resourceType) {
resourceFeed.groups.push({
...g,
title: g.group,
name: slug(g.group),
author: g.author || null,
scope: g.scope || null,
resources: [],
resourceIds: g.resources || [],
view: g.view || FEED_VIEWS.TILE,
recent: g.recent || null,
type: resourceType,
direction: g.direction || FEED_DIRECTION.HORIZONTAL,
id: crypto.createHash("sha256").update(
`${language}-${resourceType}-${g.group}`
).digest("hex")
})
})

Expand Down Expand Up @@ -158,13 +258,17 @@ let processResources = async function (resourceType) {
const recentFeedGroup = resourceFeed.groups.find(g => g.recent)

if (recentFeedGroup && recentFeedGroup.group) {

const recentFeedGroupAPI = {
title: recentFeedGroup.group,
name: slug(recentFeedGroup.group),
view: recentFeedGroup.view || FEED_VIEWS.SQUARE,
scope: recentFeedGroup.scope || FEED_SCOPES.RESOURCE,
resources: [],
type: resourceType,
direction: recentFeedGroup.direction || FEED_DIRECTION.HORIZONTAL,
id: crypto.createHash("sha256").update(
`${language}-${resourceType}-recent`
).digest("hex")
}
await database.collection(FIREBASE_DATABASE_LANGUAGES).doc(language).collection("feed").doc("recent").set(recentFeedGroupAPI);
}
Expand All @@ -182,6 +286,13 @@ let processResources = async function (resourceType) {
return g
})

for (let feedGroup of resourceFeed.groups) {
let feedGroupAll = { ...feedGroup }
feedGroupAll.direction = FEED_DIRECTION.VERTICAL
delete feedGroupAll.backgroundColor
fs.outputFileSync(`${API_DIST}/${language}/${resourceType}/feeds/${feedGroup.name}/index.json`, JSON.stringify(feedGroupAll))
}

// TODO: limit per composite feed
// iterate over resource feed
// if item resources more than X
Expand Down
3 changes: 2 additions & 1 deletion ops/deploy/deploy-sections.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ let processSections = async function (resourceType) {

for (let resource of resources) {
let totalDocuments = 0
const resourceInfo = await getResourceInfo(`${SOURCE_DIR}/${resource}`)
const resourceInfo = await getResourceInfo(`${SOURCE_DIR}/${resource}`, 1)
const resourcePathInfo = parseResourcePath(`${SOURCE_DIR}/${resource}`)
const resourceContentPath = `${SOURCE_DIR}/${resourcePathInfo.language}/${resourcePathInfo.type}/${resourcePathInfo.title}/${RESOURCE_CONTENT_DIRNAME}`
const languageInfo = await getLanguageInfo(resourcePathInfo.language)
Expand Down Expand Up @@ -105,6 +105,7 @@ let processSections = async function (resourceType) {
}

resourceInfo.sections = Object.values(resourceSectionData)
// TODO: consider number of sections and type of resource to determine the section view
resourceInfo.sectionView = totalDocuments < 100 ? SECTION_VIEWS.NORMAL : SECTION_VIEWS.DROPDOWN

fs.outputFileSync(`${API_DIST}/${resourcePathInfo.language}/${resourceType}/${resourcePathInfo.title}/${SECTION_DIRNAME}/index.json`, JSON.stringify(resourceInfo))
Expand Down
8 changes: 4 additions & 4 deletions ops/helpers/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ marked.use({
]
});

let parseBlock = async function (block, resourcePath, index, parentId) {
let parseBlock = async function (block, resourcePath, index, parentId, depth) {
let blockStyleReturn = style(block)
let blockStyle = blockStyleReturn.blockStyle

Expand Down Expand Up @@ -48,7 +48,7 @@ let parseBlock = async function (block, resourcePath, index, parentId) {
}

if (supportedBlockTypes[block.type]) {
let processedBlock = await supportedBlockTypes[block.type].process(block, resourcePath)
let processedBlock = await supportedBlockTypes[block.type].process(block, resourcePath, depth)

// In certain cases we might decide that we should skip this block, i.e image is referencing local file that
// does not exist. In this case we will skip it
Expand All @@ -61,13 +61,13 @@ let parseBlock = async function (block, resourcePath, index, parentId) {
return null
}

let parseSegment = async function (segment, resourcePath, parentId, filter = (b) => b.type !== "space") {
let parseSegment = async function (segment, resourcePath, parentId, depth, filter = (b) => b.type !== "space") {
const blocks = marked.lexer(segment).filter(filter)
let blocksData = []

for (let [index, block] of blocks.entries()) {
let indexHash = blocks.filter(b => b.type === block.type).findIndex(b => b === block)
let blockData = await parseBlock(block, resourcePath, indexHash >= 0 ? indexHash : index, parentId ?? "root")
let blockData = await parseBlock(block, resourcePath, indexHash >= 0 ? indexHash : index, parentId ?? "root", depth)

if (blockData) {
blocksData.push(blockData)
Expand Down
24 changes: 20 additions & 4 deletions ops/helpers/blocks/blockquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ export const blockquote = {
extension: {},
process: async function (block, resourcePath) {
let blockquote = { id: block.id, type: block.type }

// memory-verse
const memoryVerseRegex = /<p>([^<>]+)<\/p>/g
let memoryVerse = memoryVerseRegex.exec(block.text)
if (memoryVerse) {
block.text = block.text.replace(memoryVerseRegex, "").trim()
block.text = `**${memoryVerse[1]}**\n\n${block.text}`
blockquote.memoryVerse = true
blockquote.caption = memoryVerse[1]
}

// citation
const citationRegex = /<cite>([^<>]+)<\/cite>/g
let citation = citationRegex.exec(block.text)
if (citation) {
block.text = block.text.replace(citationRegex, "").trim()
blockquote.caption = citation[1]
block.text = `${block.text}\n\n_${citation[1]}_`
blockquote.citation = true
}

Expand All @@ -27,10 +28,25 @@ export const blockquote = {
let callout = calloutRegex.exec(block.text)
if (callout) {
block.text = block.text.replace(calloutRegex, "").trim()
blockquote.caption = callout[1]
block.text = `${block.text}\n\n_${callout[1]}_`
blockquote.callout = true
}

return {...blockquote, items: await parseSegment(block.text, resourcePath, block.id) }
let items = await parseSegment(block.text, resourcePath, block.id)

if (blockquote.callout) {
items.map((item, i) => {
if (item.type === "paragraph") {
item.style = item.style || {}
item.style.text = item.style.text || {}
item.style.text.align = "center"
if (i < items.length-1) {
item.style.text.size = "xl"
}
}
})
}

return {...blockquote, items }
}
}
2 changes: 1 addition & 1 deletion ops/helpers/blocks/excerpt.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const excerpt = {
}
markdown = markdown.replace(/\n\n$/mg, '')

item.items = await parseSegment(markdown, resourcePath, item.id)
item.items = await parseSegment(markdown, resourcePath, item.id, "no-bible")

if (item.items.length && item.items[0].type === "heading") {
item.items.shift()
Expand Down
6 changes: 3 additions & 3 deletions ops/helpers/blocks/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { parseBlock } from "../blocks.js"

export const list = {
extension: {},
process: async function (block, resourcePath) {
process: async function (block, resourcePath, depth) {
let blockData = { id: block.id, type: block.type, items: [], ordered: block.ordered, start: block.start || 0 }

for (let [index, listItem] of block.items.entries()) {
Expand All @@ -30,7 +30,7 @@ export const list = {
})
} else {
blockData.items.push(
await parseBlock(token, resourcePath, index, block.id)
await parseBlock(token, resourcePath, index, block.id, depth+1)
)
}
}
Expand Down Expand Up @@ -103,6 +103,6 @@ export const list = {
}
}

return blockData
return { ...blockData, depth }
},
}
Loading

0 comments on commit 509e58b

Please sign in to comment.