@compiiile/compiiile 2.22.0 → 2.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,20 @@ import SlidesLayout from "../layouts/SlidesLayout.astro"
6
6
  import path from "node:path"
7
7
 
8
8
  export async function getStaticPaths() {
9
- const markdownFiles = Object.values(import.meta.glob('@source/**/*.{md,mdx}', { eager: true }))
9
+ // Literal patterns to exclude which matches `IGNORED_ENTRIES` from `Context.js`
10
+ const markdownFiles = Object.values(import.meta.glob([
11
+ '@source/**/*.{md,mdx}',
12
+ '!@source/**/.vuepress/**',
13
+ '!@source/**/.git/**',
14
+ '!@source/**/node_modules/**',
15
+ '!@source/**/.compiiile/**',
16
+ '!@source/**/bin/**',
17
+ '!@source/**/.idea/**',
18
+ '!@source/**/.DS_Store/**',
19
+ '!@source/**/.gitignore/**',
20
+ '!@source/**/var/**',
21
+ '!@source/**/vendor/**',
22
+ ], { eager: true }))
10
23
 
11
24
  return routeList.map(route => {
12
25
  const md = markdownFiles.find(post => {
@@ -1,228 +1,230 @@
1
1
  import path from "node:path"
2
2
  import fs from "node:fs"
3
- import { v4 as uuidv4 } from "uuid"
3
+ import {v4 as uuidv4} from "uuid"
4
4
  import FileListItem from "./FileListItem.js"
5
5
  import FilesTreeItem from "./FilesTreeItem.js"
6
6
  import RouteListItem from "./RouteListItem.js"
7
- import { createMarkdownProcessor } from "@astrojs/markdown-remark"
8
- import { remarkPlugins, rehypePlugins, shikiConfig } from "../markdownConfig.js"
9
- import { unemojify } from "node-emoji"
7
+ import {createMarkdownProcessor} from "@astrojs/markdown-remark"
8
+ import {rehypePlugins, remarkPlugins, shikiConfig} from "../markdownConfig.js"
9
+ import {unemojify} from "node-emoji"
10
10
  import slugify from "slugify"
11
11
  import parseIgnore from "parse-gitignore"
12
- import { minimatch } from "minimatch"
12
+ import {minimatch} from "minimatch"
13
13
 
14
14
  export default class {
15
- WORKSPACE_BASE_PATH = "c"
16
- SLIDES_BASE_PATH = "s"
17
-
18
- constructor(directoryPath = ".") {
19
- this.directoryPath = directoryPath
20
- }
21
-
22
- async init(){
23
- this.fileList = []
24
- this.filesTree = []
25
- this.routeList = []
26
- this.fileRouteMap = {}
27
-
28
- this.site = {
29
- title: process.env.VITE_COMPIIILE_TITLE !== "" ? process.env.VITE_COMPIIILE_TITLE : undefined,
30
- description:
31
- process.env.VITE_COMPIIILE_DESCRIPTION !== "" ? process.env.VITE_COMPIIILE_DESCRIPTION : undefined,
32
- logo: process.env.VITE_COMPIIILE_LOGO !== "null" ? process.env.VITE_COMPIIILE_LOGO : undefined,
33
- logoUrl: process.env.VITE_COMPIIILE_LOGO_URL !== "" ? process.env.VITE_COMPIIILE_LOGO_URL : undefined,
34
- siteUrl: process.env.VITE_COMPIIILE_SITE_URL !== "" ? process.env.VITE_COMPIIILE_SITE_URL : undefined,
35
- base: process.env.VITE_COMPIIILE_BASE,
36
- data: JSON.parse(process.env.VITE_COMPIIILE_DATA),
37
- theme: process.env.VITE_COMPIIILE_THEME,
38
- collapseFolders: JSON.parse(process.env.VITE_COMPIIILE_COLLAPSE_FOLDERS)
39
- }
40
-
41
-
42
- this.filesTree = await this.scanDirectoryRecursively(this.directoryPath)
43
- return this
44
- }
45
-
46
- generateRoutePathFromFilePath(filePath, hash = "", asSlides = false, entryFileMatcher) {
47
- const filePathWithoutExtension = filePath.substring(0, filePath.lastIndexOf("."))
48
- const sluggifiedPath = filePathWithoutExtension
49
- .split("/")
50
- .map((val) => slugify(val, { lower: true }))
51
- .join("/")
52
-
53
- if (sluggifiedPath.match(new RegExp(/^/.source + entryFileMatcher.source))) {
54
- if (process.env.VITE_COMPIIILE_BASE !== "/") {
55
- return process.env.VITE_COMPIIILE_BASE
56
- }
57
-
58
- return "/"
59
- }
60
- return `${
61
- process.env.VITE_COMPIIILE_BASE.endsWith("/")
62
- ? process.env.VITE_COMPIIILE_BASE.slice(0, -1)
63
- : process.env.VITE_COMPIIILE_BASE
64
- }/${asSlides ? this.SLIDES_BASE_PATH : this.WORKSPACE_BASE_PATH}/${sluggifiedPath}${hash}`
65
- }
66
-
67
- getFileTitleFromProcessedMarkdown(processedMarkdown) {
68
- let firstHeading = null
69
- if (JSON.parse(process.env.VITE_COMPIIILE_USE_AUTO_TITLES) && processedMarkdown.metadata.headings.length > 0) {
70
- let firstHeadingIndex = 0
71
- if (Object.keys(processedMarkdown.metadata.frontmatter).length > 0) {
72
- // If a frontmatter is set, it is present as the first index in the `headings` array
73
- firstHeadingIndex = 1
74
- }
75
- // Remove the starting '#' from the title
76
- firstHeading = processedMarkdown.metadata.headings[firstHeadingIndex]?.text?.slice(1)
77
- }
78
-
79
- return processedMarkdown.metadata.frontmatter.title || firstHeading
80
- }
81
-
82
- getEntryFileMatcher(files) {
83
- return files.find((file) => file.toLowerCase().match(/^readme.mdx?$/)) ? /readme/ : /index/
84
- }
85
-
86
- async scanDirectoryRecursively(directoryPath) {
87
- const fileArray = []
88
-
89
- const collator = new Intl.Collator(undefined, {
90
- numeric: true,
91
- sensitivity: "base"
92
- })
93
-
94
- const files = fs.readdirSync(directoryPath).sort(collator.compare)
95
-
96
- const entryFileMatcher = this.getEntryFileMatcher(files)
97
-
98
- for (let file of files) {
99
- if (
100
- ![
101
- ".vuepress",
102
- ".git",
103
- "node_modules",
104
- ".compiiile",
105
- "bin",
106
- ".idea",
107
- ".DS_Store",
108
- ".gitignore",
109
- "vendor"
110
- ].includes(file)
111
- ) {
112
- const filePath = path.join(directoryPath, file)
113
-
114
- try {
115
- const compiiileIgnoreFilePath = fs.readFileSync(
116
- path.join(process.env.COMPIIILE_SOURCE, ".compiiileignore")
117
- )
118
- const compiiileIgnore = parseIgnore(compiiileIgnoreFilePath)
119
-
120
- const ignoreGlobs = compiiileIgnore.globs().filter((globType) => globType.type === "ignore")
121
- const ignorePatterns = ignoreGlobs.reduce((patterns, currentIgnoreGlobItem) => {
122
- return [...patterns, ...currentIgnoreGlobItem.patterns]
123
- }, [])
124
-
125
- const isFilePathInIgnoredPatterns = ignorePatterns.some((pattern) => minimatch(filePath, pattern))
126
-
127
- if (isFilePathInIgnoredPatterns) {
128
- continue
129
- }
130
- } catch (_) {
131
- // No .compiiileignore file found at the root
132
- }
133
-
134
- const isDirectory = fs.statSync(filePath).isDirectory()
135
- const uuid = uuidv4()
136
- const fileName = path.parse(filePath).name
137
- const isReadmeFile =
138
- !isDirectory &&
139
- filePath
140
- .toLowerCase()
141
- .match(new RegExp(/^(.*?\/?)/.source + entryFileMatcher.source + /\.mdx?$/.source))
142
-
143
- let filesTreeItem = new FilesTreeItem(uuid, fileName)
144
-
145
- if (isDirectory) {
146
- filesTreeItem.isDirectory = true
147
- filesTreeItem.children = await this.scanDirectoryRecursively(filePath)
148
- }
149
-
150
- if (path.extname(file).match(/^\.mdx?$/) || filesTreeItem?.children.length > 0) {
151
- if (isReadmeFile) {
152
- fileArray.unshift(filesTreeItem)
153
- } else {
154
- fileArray.push(filesTreeItem)
155
- }
156
-
157
- if (!isDirectory) {
158
- const fileListItem = new FileListItem(uuid)
159
- const markdownContent = fs.readFileSync(filePath, {
160
- encoding: "utf8"
161
- })
162
-
163
- const markdownProcessor = await createMarkdownProcessor({
164
- remarkPlugins,
165
- rehypePlugins: rehypePlugins(this),
166
- shikiConfig
167
- })
168
- const renderedMarkdown = await markdownProcessor.render(markdownContent)
169
-
170
- fileListItem.textContent = unemojify(
171
- renderedMarkdown.code
172
- .replace(/^<hr>\n<h2(.|\n)*?<\/h2>/g, "")
173
- .replace(/<br>/g, " ")
174
- .replace(/<a.*aria-hidden.*>.*?<\/a>|<[^>]*>?/gi, "")
175
- .replace(/[\r\n]{2,}/g, "\n")
176
- )
177
-
178
- const meta = renderedMarkdown.metadata.frontmatter
179
-
180
- if (meta.ignore) {
181
- const fileIndex = fileArray.findIndex((f) => f.uuid === fileListItem.uuid)
182
- if (fileIndex > -1) {
183
- fileArray.splice(fileIndex, 1)
184
- }
185
- continue
186
- }
187
-
188
- const title = this.getFileTitleFromProcessedMarkdown(renderedMarkdown) || fileName
189
- fileListItem.title = title
190
- fileListItem.meta = meta
191
- fileListItem.meta.title = title
192
- fileListItem.fullPath = filePath
193
-
194
- const routePath = this.generateRoutePathFromFilePath(
195
- filePath,
196
- "",
197
- fileListItem.meta.asSlides,
198
- entryFileMatcher
199
- )
200
-
201
- if (isReadmeFile) {
202
- let newIndex = 0
203
-
204
- if (directoryPath !== ".") {
205
- newIndex = this.fileList.findIndex((f) => f.fullPath.startsWith(directoryPath))
206
- if (newIndex < 0) {
207
- newIndex = this.fileList.length
208
- }
209
- }
210
-
211
- this.fileList.splice(newIndex, 0, fileListItem)
212
- } else {
213
- this.fileList.push(fileListItem)
214
- }
215
-
216
- // `asSlides` frontmatter param is passed via the meta
217
- const routeListItem = new RouteListItem(routePath, uuid, fileName, filePath, fileListItem.meta)
218
- fileListItem.routePath = routeListItem.path
219
- this.fileRouteMap[filePath] = routeListItem.path
220
- this.routeList.push(routeListItem)
221
- }
222
- }
223
- }
224
- }
225
-
226
- return fileArray
227
- }
15
+ WORKSPACE_BASE_PATH = "c"
16
+ SLIDES_BASE_PATH = "s"
17
+
18
+ constructor(directoryPath = ".") {
19
+ this.directoryPath = directoryPath
20
+ }
21
+
22
+ async init() {
23
+ this.fileList = []
24
+ this.filesTree = []
25
+ this.routeList = []
26
+ this.fileRouteMap = {}
27
+
28
+ this.site = {
29
+ title: process.env.VITE_COMPIIILE_TITLE !== "" ? process.env.VITE_COMPIIILE_TITLE : undefined,
30
+ description:
31
+ process.env.VITE_COMPIIILE_DESCRIPTION !== "" ? process.env.VITE_COMPIIILE_DESCRIPTION : undefined,
32
+ logo: process.env.VITE_COMPIIILE_LOGO !== "null" ? process.env.VITE_COMPIIILE_LOGO : undefined,
33
+ logoUrl: process.env.VITE_COMPIIILE_LOGO_URL !== "" ? process.env.VITE_COMPIIILE_LOGO_URL : undefined,
34
+ siteUrl: process.env.VITE_COMPIIILE_SITE_URL !== "" ? process.env.VITE_COMPIIILE_SITE_URL : undefined,
35
+ base: process.env.VITE_COMPIIILE_BASE,
36
+ data: JSON.parse(process.env.VITE_COMPIIILE_DATA),
37
+ theme: process.env.VITE_COMPIIILE_THEME,
38
+ collapseFolders: JSON.parse(process.env.VITE_COMPIIILE_COLLAPSE_FOLDERS)
39
+ }
40
+
41
+
42
+ this.filesTree = await this.scanDirectoryRecursively(this.directoryPath)
43
+ return this
44
+ }
45
+
46
+ generateRoutePathFromFilePath(filePath, hash = "", asSlides = false, entryFileMatcher) {
47
+ const filePathWithoutExtension = filePath.substring(0, filePath.lastIndexOf("."))
48
+ const sluggifiedPath = filePathWithoutExtension
49
+ .split("/")
50
+ .map((val) => slugify(val, {lower: true}))
51
+ .join("/")
52
+
53
+ if (sluggifiedPath.match(new RegExp(/^/.source + entryFileMatcher.source))) {
54
+ if (process.env.VITE_COMPIIILE_BASE !== "/") {
55
+ return process.env.VITE_COMPIIILE_BASE
56
+ }
57
+
58
+ return "/"
59
+ }
60
+ return `${
61
+ process.env.VITE_COMPIIILE_BASE.endsWith("/")
62
+ ? process.env.VITE_COMPIIILE_BASE.slice(0, -1)
63
+ : process.env.VITE_COMPIIILE_BASE
64
+ }/${asSlides ? this.SLIDES_BASE_PATH : this.WORKSPACE_BASE_PATH}/${sluggifiedPath}${hash}`
65
+ }
66
+
67
+ getFileTitleFromProcessedMarkdown(processedMarkdown) {
68
+ let firstHeading = null
69
+ if (JSON.parse(process.env.VITE_COMPIIILE_USE_AUTO_TITLES) && processedMarkdown.metadata.headings.length > 0) {
70
+ let firstHeadingIndex = 0
71
+ if (Object.keys(processedMarkdown.metadata.frontmatter).length > 0) {
72
+ // If a frontmatter is set, it is present as the first index in the `headings` array
73
+ firstHeadingIndex = 1
74
+ }
75
+ // Remove the starting '#' from the title
76
+ firstHeading = processedMarkdown.metadata.headings[firstHeadingIndex]?.text?.slice(1)
77
+ }
78
+
79
+ return processedMarkdown.metadata.frontmatter.title || firstHeading
80
+ }
81
+
82
+ getEntryFileMatcher(files) {
83
+ return files.find((file) => file.toLowerCase().match(/^readme.mdx?$/)) ? /readme/ : /index/
84
+ }
85
+
86
+ async scanDirectoryRecursively(directoryPath) {
87
+ const fileArray = []
88
+
89
+ const collator = new Intl.Collator(undefined, {
90
+ numeric: true,
91
+ sensitivity: "base"
92
+ })
93
+
94
+ const IGNORED_ENTRIES = new Set([
95
+ ".vuepress",
96
+ ".git",
97
+ "node_modules",
98
+ ".compiiile",
99
+ "bin",
100
+ ".idea",
101
+ ".DS_Store",
102
+ ".gitignore",
103
+ "var",
104
+ "vendor"
105
+ ])
106
+
107
+ const files = fs.readdirSync(directoryPath)
108
+ .filter((file) => !IGNORED_ENTRIES.has(file))
109
+ .sort(collator.compare)
110
+
111
+ const entryFileMatcher = this.getEntryFileMatcher(files)
112
+
113
+ for (let file of files) {
114
+ const filePath = path.join(directoryPath, file)
115
+
116
+ try {
117
+ const compiiileIgnoreFilePath = fs.readFileSync(
118
+ path.join(process.env.COMPIIILE_SOURCE, ".compiiileignore")
119
+ )
120
+ const compiiileIgnore = parseIgnore(compiiileIgnoreFilePath)
121
+
122
+ const ignoreGlobs = compiiileIgnore.globs().filter((globType) => globType.type === "ignore")
123
+ const ignorePatterns = ignoreGlobs.reduce((patterns, currentIgnoreGlobItem) => {
124
+ return [...patterns, ...currentIgnoreGlobItem.patterns]
125
+ }, [])
126
+
127
+ const isFilePathInIgnoredPatterns = ignorePatterns.some((pattern) => minimatch(filePath, pattern))
128
+
129
+ if (isFilePathInIgnoredPatterns) {
130
+ continue
131
+ }
132
+ } catch (_) {
133
+ // No .compiiileignore file found at the root
134
+ }
135
+
136
+ const isDirectory = fs.statSync(filePath).isDirectory()
137
+ const uuid = uuidv4()
138
+ const fileName = path.parse(filePath).name
139
+ const isReadmeFile =
140
+ !isDirectory &&
141
+ filePath
142
+ .toLowerCase()
143
+ .match(new RegExp(/^(.*?\/?)/.source + entryFileMatcher.source + /\.mdx?$/.source))
144
+
145
+ let filesTreeItem = new FilesTreeItem(uuid, fileName)
146
+
147
+ if (isDirectory) {
148
+ filesTreeItem.isDirectory = true
149
+ filesTreeItem.children = await this.scanDirectoryRecursively(filePath)
150
+ }
151
+
152
+ if (path.extname(file).match(/^\.mdx?$/) || filesTreeItem?.children.length > 0) {
153
+ if (isReadmeFile) {
154
+ fileArray.unshift(filesTreeItem)
155
+ } else {
156
+ fileArray.push(filesTreeItem)
157
+ }
158
+
159
+ if (!isDirectory) {
160
+ const fileListItem = new FileListItem(uuid)
161
+ const markdownContent = fs.readFileSync(filePath, {
162
+ encoding: "utf8"
163
+ })
164
+
165
+ const markdownProcessor = await createMarkdownProcessor({
166
+ remarkPlugins,
167
+ rehypePlugins: rehypePlugins(this),
168
+ shikiConfig
169
+ })
170
+ const renderedMarkdown = await markdownProcessor.render(markdownContent)
171
+
172
+ fileListItem.textContent = unemojify(
173
+ renderedMarkdown.code
174
+ .replace(/^<hr>\n<h2(.|\n)*?<\/h2>/g, "")
175
+ .replace(/<br>/g, " ")
176
+ .replace(/<a.*aria-hidden.*>.*?<\/a>|<[^>]*>?/gi, "")
177
+ .replace(/[\r\n]{2,}/g, "\n")
178
+ )
179
+
180
+ const meta = renderedMarkdown.metadata.frontmatter
181
+
182
+ if (meta.ignore) {
183
+ const fileIndex = fileArray.findIndex((f) => f.uuid === fileListItem.uuid)
184
+ if (fileIndex > -1) {
185
+ fileArray.splice(fileIndex, 1)
186
+ }
187
+ continue
188
+ }
189
+
190
+ const title = this.getFileTitleFromProcessedMarkdown(renderedMarkdown) || fileName
191
+ fileListItem.title = title
192
+ fileListItem.meta = meta
193
+ fileListItem.meta.title = title
194
+ fileListItem.fullPath = filePath
195
+
196
+ const routePath = this.generateRoutePathFromFilePath(
197
+ filePath,
198
+ "",
199
+ fileListItem.meta.asSlides,
200
+ entryFileMatcher
201
+ )
202
+
203
+ if (isReadmeFile) {
204
+ let newIndex = 0
205
+
206
+ if (directoryPath !== ".") {
207
+ newIndex = this.fileList.findIndex((f) => f.fullPath.startsWith(directoryPath))
208
+ if (newIndex < 0) {
209
+ newIndex = this.fileList.length
210
+ }
211
+ }
212
+
213
+ this.fileList.splice(newIndex, 0, fileListItem)
214
+ } else {
215
+ this.fileList.push(fileListItem)
216
+ }
217
+
218
+ // `asSlides` frontmatter param is passed via the meta
219
+ const routeListItem = new RouteListItem(routePath, uuid, fileName, filePath, fileListItem.meta)
220
+ fileListItem.routePath = routeListItem.path
221
+ this.fileRouteMap[filePath] = routeListItem.path
222
+ this.routeList.push(routeListItem)
223
+ }
224
+ }
225
+
226
+ }
227
+
228
+ return fileArray
229
+ }
228
230
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@compiiile/compiiile",
3
3
  "private": false,
4
- "version": "2.22.0",
4
+ "version": "2.22.1",
5
5
  "description": "The most convenient way to render a folder containing markdown files. Previewing and searching markdown files has never been that easy.",
6
6
  "author": "AlbanCrepel <alban.crepel@gmail.com>",
7
7
  "license": "GPL-3.0-only",