Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix content language and serialize parse #388

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/core/database.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import Loki from '@lokidb/loki'
import { QueryBuilder } from './runtime/api/QueryBuilder'
import { useHooks } from './hooks'

let _db
let _items
export const useDB = () => {
const { callHook } = useHooks()
if (!_db) {
_db = new Loki('docus.db')
_items = _db.addCollection('items', {})
Expand All @@ -15,8 +13,7 @@ export const useDB = () => {
items: _items,
query: createQuery,
find,
async insert(document) {
await callHook('docus:storage:beforeInsert', document)
insert(document) {
const existed = _items.findOne({ key: document.key })

if (existed) {
Expand Down
22 changes: 12 additions & 10 deletions src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ export default <Module>async function docusModule() {
initParser(parserOptions)

const coreHooks = useHooks()

// Configure content after each hook
// Locales or empty array
let locales = options.i18n?.locales || []
// If locales is function, resolve it
locales = typeof locales === 'function' ? locales() : locales
// Map locales or default to 'en'
locales = locales.map(({ code }: { code: string }) => code).join('|') || 'en'
// Get default locale or default to 'en'
const defaultLocale = options.i18n?.defaultLocale || 'en'
const regexp = new RegExp(`^/(${locales})`, 'gi')

coreHooks.hook('docus:storage:beforeInsert', (document: DocusDocument) => {
if (document.extension !== '.md') {
return
}

// Locales or empty array
let locales = options.i18n?.locales || []
// If locales is function, resolve it
locales = typeof locales === 'function' ? locales() : locales
// Map locales or default to 'en'
locales = locales.map(({ code }: { code: string }) => code).join('|') || 'en'
// Get default locale or default to 'en'
const defaultLocale = options.i18n?.defaultLocale || 'en'

const regexp = new RegExp(`^/(${locales})`, 'gi')
const { dir, slug } = document
const _dir = dir.replace(regexp, '')
const _language = dir.replace(_dir, '').replace(/\//, '') || defaultLocale
Expand All @@ -81,6 +82,7 @@ export default <Module>async function docusModule() {
document.slug = generateSlug(slug)
document.position = position
document.to = generateTo(_to)
document.path = document.to
document.language = _language
document.draft = document.draft || isDraft(slug)
})
Expand Down
22 changes: 15 additions & 7 deletions src/core/storage/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DocusDocument, DriverOptions } from '../../types'
import { useDB } from '../database'
import { useHooks } from '../hooks'
import { useParser } from '../parser'
import { logger } from '../utils'

export interface DocusDriver extends Driver {
init(): Promise<void>
Expand Down Expand Up @@ -52,6 +53,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
}

const { insert, items } = useDB()
const { callHook } = useHooks()
const parser = useParser()
const fs = fsDriver(options)

Expand All @@ -70,13 +72,16 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
// Unify key format
document.key = key

// use prefix in document path
document.path = `/${options.mountPoint}` + document.path

// Enrich document layout based on parents data
const parents = await getItemParents(key)
document.layout = defu(document.layout, ...parents.map(p => p.layout))

// call beforeInsert Hook
await callHook('docus:storage:beforeInsert', document)

// use prefix in document path
document.path = `/${options.mountPoint}` + document.path

return insert(document)
}

Expand Down Expand Up @@ -147,6 +152,8 @@ export const docusDriver = defineDriver((options: DriverOptions) => {

// Read contents and initialize database
const init = async () => {
const start = Date.now()
const end = () => Date.now() - start
// ensure directory exists
if (!fs.hasItem('')) {
return
Expand All @@ -157,15 +164,16 @@ export const docusDriver = defineDriver((options: DriverOptions) => {

// sort keys to parse index files before others
keys = sortItemKeys(keys)
const total = keys.length

const tasks = keys.map(key => fs.getItem(key).then(content => parseAndInsert(key, content)))

await Promise.all(tasks)
while (keys.length) {
await Promise.all(keys.splice(0, 8).map(key => fs.getItem(key).then(content => parseAndInsert(key, content))))
}
logger.info(`${total} files processed in ${end()}ms`)
}

// Watch files and revalidate data
const watch = callback => {
const { callHook } = useHooks()
return fs.watch(async (event, key) => {
if (event === 'update') {
const content = await fs.getItem(key)
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function updateNavigation(nuxt) {
}

// Query pages
const pages = await query('/page', { deep: true }).where(where).only(fields).sortBy('position', 'asc').fetch()
const pages = await query('/pages', { deep: true }).where(where).only(fields).sortBy('position', 'asc').fetch()

const languages: { [key: string]: any[] } = pages.reduce((map, page) => {
const language = page.language || defaultLocale
Expand Down