UNPKG

2.33 kBJavaScriptView Raw
1const logger = require('consola').withScope('@nuxt/content')
2const { camelCase } = require('change-case')
3
4const getDefaults = ({ dev = false } = {}) => ({
5 editor: './editor.vue',
6 watch: dev,
7 liveEdit: true,
8 apiPrefix: '_content',
9 dir: 'content',
10 fullTextSearchFields: ['title', 'description', 'slug', 'text'],
11 nestedProperties: [],
12 markdown: {
13 tocDepth: 3,
14 remarkPlugins: [
15 'remark-squeeze-paragraphs',
16 'remark-slug',
17 'remark-autolink-headings',
18 'remark-external-links',
19 'remark-footnotes',
20 'remark-gfm'
21 ],
22 rehypePlugins: [
23 'rehype-sort-attribute-values',
24 'rehype-sort-attributes',
25 'rehype-raw'
26 ],
27 prism: {
28 theme: 'prismjs/themes/prism.css'
29 }
30 },
31 yaml: {},
32 csv: {},
33 xml: {},
34 extendParser: {}
35})
36
37const processMarkdownTocDepth = (markdown) => {
38 const { tocDepth } = markdown
39 const tocTags = []
40
41 if (tocDepth < 1) {
42 logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will be empty.`)
43 return tocTags
44 }
45
46 if (tocDepth > 6) {
47 logger.info(`content.markdown.tocDepth is set as ${tocDepth}. Table of contents of markdown files will include all the headings.`)
48 }
49
50 for (let i = 2; i <= tocDepth; i++) {
51 tocTags.push(`h${i}`)
52 }
53
54 return tocTags
55}
56
57const processMarkdownPlugins = (type, markdown, resolvePath) => {
58 const plugins = []
59
60 for (const plugin of markdown[`${type}Plugins`]) {
61 let name
62 let options
63 let instance
64
65 if (typeof plugin === 'string') {
66 name = plugin
67 options = markdown[camelCase(name)]
68 } else if (Array.isArray(plugin)) {
69 [name, options] = plugin
70 }
71
72 try {
73 instance = require(resolvePath(name))
74
75 plugins.push({ instance, name, options })
76 } catch (e) {
77 logger.error(e.toString())
78 }
79 }
80
81 return plugins
82}
83
84const processMarkdownOptions = (options, resolvePath) => {
85 if (!resolvePath) {
86 resolvePath = path => path
87 }
88 options.markdown.tocTags = processMarkdownTocDepth(options.markdown)
89 options.markdown.remarkPlugins = processMarkdownPlugins('remark', options.markdown, resolvePath)
90 options.markdown.rehypePlugins = processMarkdownPlugins('rehype', options.markdown, resolvePath)
91}
92
93module.exports = {
94 getDefaults,
95 processMarkdownOptions
96}