Skip to content

Commit

Permalink
feat: change ignore behavior (#448)
Browse files Browse the repository at this point in the history
* fix: change ignore behavior

* chore: update docs

* docs: document new system

* fix: generateSlug

* fix: do not render hidden contents as a standalone page

* docs: update docs
  • Loading branch information
farnabaz authored Jun 17, 2021
1 parent 2cf5c0d commit 4d8ef34
Show file tree
Hide file tree
Showing 209 changed files with 99 additions and 24 deletions.
27 changes: 23 additions & 4 deletions docs/content/3.features/1.routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ content/
2.nuxt.md
```

## Filtering
## Ignoring

If you are working on a draft that you do not want to be displayed from the navigation, just prefix it with "_".
If you are working on a draft that you do not want to be list in the navigation or accessible via as a separate page, just prefix it with `"."`.

It will ignore this file and it will not be accessible unless you're in **development** or **admin** mode.

Expand All @@ -51,9 +51,28 @@ You can still use the content from these files using [`InjectContent`](/theme/co
```
content/
1.examples/
_hidden.md // Hidden file
_2.frameworks/ // Hidden directory
.hidden.md // Ignored file
.2.frameworks/ // Ignored directory
hidden.md
```

> You can take a look at our [docs content folder](https://github.com/nuxt/content/tree/dev/docs/content/en) to see an example

## Non-Page Contents

If you have some files that you don't want to have them as a separated pages but you want search among them, just prefix it with `"_"`.

The only way to access these files is to use `$docus.search` API or `:InjectContetn` component.

```
content/
1.examples/
_hidden.md // Filtered file
_2.frameworks/ // Filters directory
hidden.md
```

::alert
**Note**: The beginning `_` will remove from document path, and you don't need to use `_` in search queries.
::
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion src/app/pages/_.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export default defineComponent({
const draft = false
// Page query
const [page] = await $docus.search({ deep: true }).where({ language, to, draft }).fetch()
const [page] = await $docus
.search({ deep: true })
.where({ language, to, draft, page: { $ne: false } })
.fetch()
// Break on missing page query
if (!page) return error({ statusCode: 404, message: 'Page not found' })
Expand Down
17 changes: 16 additions & 1 deletion src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Module } from '@nuxt/types'
import hash from 'hasha'
import mkdirp from 'mkdirp'
import { DocusDocument, ParserOptions } from '../types'
import { generatePosition, generateSlug, generateTo, isDraft, processDocumentInfo } from './utils/document'
import { generatePosition, generateSlug, generateTo, isDraft, isHidden, processDocumentInfo } from './utils/document'
import { destroyStorage, initStorage, useNuxtIgnoreList } from './storage'
import { destroyDB, useDB } from './database'
import { createServerMiddleware } from './server'
Expand Down Expand Up @@ -79,6 +79,21 @@ export default <Module>async function docusModule() {

processDocumentInfo(document)

/**
* Disable document navigation if it is marked as `page = false`
* This will prevent showing non-pages in navigation menus
*/
if (document.page === false) {
document.navigation = false
}

if (isHidden(_to)) {
// Do not show document on navigation menus
document.navigation = false
// Do not render document as standalone page
document.page = false
}

document.slug = generateSlug(slug)
document.position = position
document.to = generateTo(_to)
Expand Down
6 changes: 3 additions & 3 deletions src/core/storage/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ function sortItemKeys(keys: string[]) {
}

export const docusDriver = defineDriver((options: DriverOptions) => {
// force ignore node_modules and .git and files with `_` prefix
// force ignore node_modules and .git and files with `.` prefix
if (options.ignore) {
options.ignore.push('**/node_modules/**', '**/.git/**', join(options.base, '**/_**/**'))
options.ignore.push('**/node_modules/**', '**/.git/**', join(options.base, '**/.**/**'))
}

const { insert, items } = useDB()
Expand Down Expand Up @@ -93,7 +93,7 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
* Also parent will be used to group up children in bottom navigation (prev/next page)
*/
if (document.navigation !== false) {
const exclusiveParent = parents.find(p => p.navigation?.exclusive)
const exclusiveParent = parents.find(p => p.navigation && p.navigation.exclusive)
if (exclusiveParent) {
document.navigation = document.navigation || {}
// Store nearest parent path
Expand Down
35 changes: 31 additions & 4 deletions src/core/utils/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ export function generatePosition(path: string, document: DocusDocument): string
return padRight(position, 12)
}

/**
* Clean up special keywords from path part
*/
export function generateSlug(name: string): string {
return name
.replace(/(\d+\.)?(.*)/, '$2')
.replace(/^index/, '')
.replace(/\.draft/, '')
return (
name
/**
* Remove hidden keyword
*/
.replace(/^_/, '')
/**
* Remove numbering
*/
.replace(/(\d+\.)?(.*)/, '$2')
/**
* remove index keyword
*/
.replace(/^index/, '')
/**
* remove draft keyword
*/
.replace(/\.draft/, '')
)
}

export function generateTo(path: string): string {
Expand All @@ -32,6 +50,15 @@ export function isDraft(path: string): boolean {
return !!path.match(/(\.draft)$/)
}

/**
* Files or directories that starts with underscore `_` will mark as hidden
* @param path content path
* @returns true if the is part in the path that starts with `_`
*/
export function isHidden(path: string): boolean {
return path.split('/').some(part => part.match(/^_.*/))
}

export function processDocumentInfo(document: DocusDocument): DocusDocument {
// There is no need to extract if both title and descriptio is provided by user
if (document.title && document.description) return document
Expand Down
33 changes: 22 additions & 11 deletions src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,10 @@ export interface DocusDocument {
position: string
draft: boolean
// Navigation
navigation: {
title: string
slot: string
nested: boolean
// url of nearest exclusive parent
// parent uses to filter pages to find currect previous and next page
parent: string
[key: string]: any
}
navigation: NavItemNavigationConfig | false
// url of nearest exclusive parent
// parent uses to filter pages to find currect previous and next page
parent: string
// Template
template: {
self: string
Expand All @@ -106,14 +101,30 @@ export interface DocusDocument {
descriptionNode?: any

// Generated
/**
* If set to `false` the document will not render as a standalone page an can only accessible with `InjectContent` of `$docus` search API
*/
page: boolean
/**
* The unique key of document (file path)
*/
key: string
/**
* Path of document in the storage.
*/
path: string
slug: string
/**
* Generated url of document. This url will be used to create anchor links of document.
*/
to: string
/**
* File extension
*/
extension: string
slug: string
toc: false | Toc
language: string
body: DocusRootNode
extension: string
dir: string
createdAt: Date
updatedAt: Date
Expand Down

1 comment on commit 4d8ef34

@vercel
Copy link

@vercel vercel bot commented on 4d8ef34 Jun 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.