-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue.config.ts
115 lines (106 loc) · 3.15 KB
/
vue.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { ProjectOptions } from '@vue/cli-service';
import MarkDownIt from 'markdown-it';
import fm, { FrontMatterResult } from 'front-matter';
import path from 'path';
import fs from 'fs';
import dirTree from 'directory-tree';
export interface ArticleMarkdownFile {
title: string;
}
export interface ContentLibraryDataObject {
/**
* The raw result from the front-matter library so the app can access
* markdown file frontmatter.
*/
fileContent?: FrontMatterResult<ArticleMarkdownFile>;
/**
* The parsed HTML from markdown-it which can be used to render page content.
*/
htmlContent?: string;
contentLibraryChildren?: ContentLibraryDataObject[];
type: 'directory' | 'file';
path: string;
extension?: string;
/**
* Identifier for the index this item resides in, as in,
* the directory name. This is intended to be used to aid
* resolving components in the app.
*
* When `/, this indicates the top level of the content
* directory tree.
*/
indexName: string;
}
/**
* Parses the `src/_content` directory for markdown files
* and returns the directory tree.
*/
const generateContentLibrary = (
tree: directoryTree.DirectoryTree,
): ContentLibraryDataObject => {
const markdown = new MarkDownIt();
const pathParts = tree.path
// Handle cases where the directory tree starts with `./`.
.replace('./', '')
.split('/');
let indexName: string | undefined;
let fileContent: FrontMatterResult<ArticleMarkdownFile> | undefined;
let htmlContent: string | undefined;
if (tree.type === 'directory') {
// In an array of path parts, the last item in the array is the
// directory name when the file type is 'directory'.
indexName = pathParts.length > 2 ? pathParts[pathParts.length - 1] : '/';
} else {
const file = fs.readFileSync(path.resolve(__dirname, tree.path));
fileContent = fm<ArticleMarkdownFile>(file.toString());
htmlContent = markdown.render(fileContent.body);
// If the file type is 'file', the directory name will be the second-to-last item.
indexName = pathParts[pathParts.length - 2];
}
return {
fileContent: fileContent,
htmlContent: htmlContent,
contentLibraryChildren: tree.children?.map(child =>
generateContentLibrary(child),
),
type: tree.type,
path: tree.path,
extension: tree.extension,
indexName: indexName,
};
};
const configuration: ProjectOptions = {
configureWebpack: {
module: {
rules: [
{
test: /\.md$/,
use: [
{
loader: 'raw-loader',
},
],
},
],
},
},
chainWebpack: config => {
config.plugin('copy').tap(args => {
args[0].push({
from: path.resolve(__dirname, 'public/admin/index.html'),
to: path.resolve(__dirname, 'dist/admin'),
toType: 'dir',
});
return args;
});
config.plugin('define').tap(definitions => {
definitions[0]['process.env']['CONTENT_LIBRARY'] = JSON.stringify(
generateContentLibrary(
dirTree('./src/_content', { extensions: /\.md/ }),
),
);
return definitions;
});
},
};
module.exports = configuration;