-
Notifications
You must be signed in to change notification settings - Fork 17
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
[#40, #54] Configuration file with computed fields and include/exclude file options #84
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf8636a
Add strict mode
mohamedsalem401 7f95118
Add configuration file / Include and exclude patterns
mohamedsalem401 7e42ae2
add npx markdowndb init CLI script that will bootstrap the config file
mohamedsalem401 156f79f
fix tests
mohamedsalem401 48b38c4
Remove the init command
mohamedsalem401 a27ff6f
Add default for configuration files
mohamedsalem401 b15bf09
Update to markdowndb.config.js
mohamedsalem401 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default { | ||
customFields: [] | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { FileInfo } from "./process.js"; | ||
import { Root } from "remark-parse/lib/index.js"; | ||
|
||
export interface CustomConfig { | ||
customFields?: ((fileInfo: FileInfo, ast: Root) => any)[]; | ||
include?: string[]; | ||
exclude?: string[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,76 @@ | ||
import { FileInfo, processFile } from "./process.js"; | ||
import { recursiveWalkDir } from "./recursiveWalkDir.js"; | ||
import { CustomConfig } from "./CustomConfig.js"; | ||
import micromatch from "micromatch"; | ||
|
||
export function indexFolder( | ||
folderPath: string, | ||
pathToUrlResolver: (filePath: string) => string, | ||
config: CustomConfig, | ||
ignorePatterns?: RegExp[] | ||
) { | ||
const filePathsToIndex = recursiveWalkDir(folderPath); | ||
const filteredFilePathsToIndex = filePathsToIndex.filter((filePath) => | ||
shouldIncludeFile(filePath, ignorePatterns) | ||
shouldIncludeFile({ | ||
filePath, | ||
ignorePatterns, | ||
includeGlob: config.include, | ||
excludeGlob: config.exclude, | ||
}) | ||
); | ||
const files: FileInfo[] = []; | ||
|
||
const customFields = config.customFields || []; | ||
for (const filePath of filteredFilePathsToIndex) { | ||
const fileObject = processFile( | ||
folderPath, | ||
filePath, | ||
const fileObject = processFile(folderPath, filePath, { | ||
pathToUrlResolver, | ||
filePathsToIndex | ||
); | ||
filePathsToIndex, | ||
customFields, | ||
}); | ||
files.push(fileObject); | ||
} | ||
return files; | ||
} | ||
|
||
function shouldIncludeFile( | ||
filePath: string, | ||
ignorePatterns?: RegExp[] | ||
): boolean { | ||
return !( | ||
ignorePatterns && ignorePatterns.some((pattern) => pattern.test(filePath)) | ||
); | ||
function shouldIncludeFile({ | ||
filePath, | ||
ignorePatterns, | ||
includeGlob, | ||
excludeGlob, | ||
}: { | ||
filePath: string; | ||
ignorePatterns?: RegExp[]; | ||
includeGlob?: string[]; | ||
excludeGlob?: string[]; | ||
}): boolean { | ||
const normalizedFilePath = filePath.replace(/\\/g, "/"); | ||
|
||
if ( | ||
ignorePatterns && | ||
ignorePatterns.some((pattern) => pattern.test(normalizedFilePath)) | ||
) { | ||
return false; | ||
} | ||
|
||
// Check if the file should be included based on includeGlob | ||
if ( | ||
includeGlob && | ||
!includeGlob.some((pattern) => | ||
micromatch.isMatch(normalizedFilePath, pattern) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
// Check if the file should be excluded based on excludeGlob | ||
if ( | ||
excludeGlob && | ||
excludeGlob.some((pattern) => | ||
micromatch.isMatch(normalizedFilePath, pattern) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
getUniqueValues, | ||
} from "./databaseUtils.js"; | ||
import fs from "fs"; | ||
import { CustomConfig } from "./CustomConfig.js"; | ||
|
||
const defaultFilePathToUrl = (filePath: string) => { | ||
let url = filePath | ||
|
@@ -22,7 +23,7 @@ | |
return encodeURI(url); | ||
}; | ||
|
||
const resolveLinkToUrlPath = (link: string, sourceFilePath?: string) => { | ||
if (!sourceFilePath) { | ||
return link; | ||
} | ||
|
@@ -40,14 +41,42 @@ | |
*/ | ||
export class MarkdownDB { | ||
config: Knex.Config; | ||
customConfig: CustomConfig = {}; | ||
//@ts-ignore | ||
Check failure on line 45 in src/lib/markdowndb.ts GitHub Actions / Lint & format check
|
||
db: Knex; | ||
|
||
/** | ||
* Constructs a new MarkdownDB instance. | ||
* @param {Knex.Config} config - Knex configuration object. | ||
* @param {Knex.Config} knexConfig - Knex configuration object. | ||
* @param {CustomConfig} [customConfig] - Custom configuration object. | ||
*/ | ||
constructor(config: Knex.Config) { | ||
this.config = config; | ||
constructor( | ||
knexConfig: Knex.Config, | ||
options?: { customConfig?: CustomConfig; configFilePath?: string } | ||
) { | ||
this.config = knexConfig; | ||
if (options?.customConfig) { | ||
this.customConfig = options.customConfig; | ||
} else { | ||
this.loadConfiguration(options?.configFilePath); | ||
} | ||
} | ||
|
||
private async loadConfiguration(configFilePath?: string) { | ||
const normalizedPath = path.resolve(configFilePath || "markdowndb.config.js"); | ||
const fileUrl = new URL(`file://${normalizedPath}`); | ||
|
||
try { | ||
// Import the module using the file URL | ||
const configModule = await import(fileUrl.href); | ||
this.customConfig = configModule.default; | ||
} catch (error: any) { | ||
if (configFilePath) { | ||
throw new Error( | ||
`Error loading configuration file from ${normalizedPath}: ${error.message}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -82,6 +111,7 @@ | |
const fileObjects = indexFolder( | ||
folderPath, | ||
pathToUrlResolver, | ||
this.customConfig, | ||
ignorePatterns | ||
); | ||
const filesToInsert = fileObjects.map(mapFileToInsert); | ||
|
@@ -248,7 +278,7 @@ | |
} | ||
} | ||
|
||
function writeJsonToFile(filePath: string, jsonData: any[]) { | ||
try { | ||
const directory = path.dirname(filePath); | ||
if (!fs.existsSync(directory)) { | ||
|
@@ -257,7 +287,7 @@ | |
|
||
const jsonString = JSON.stringify(jsonData, null, 2); | ||
fs.writeFileSync(filePath, jsonString); | ||
} catch (error: any) { | ||
console.error(`Error writing data to ${filePath}: ${error}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you don't have a config file - this stuff should work with defaults ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rufuspollock
OK, but what should be defaulted?
Currently, it defaults to searching for
markdowndb.config.js
if no config path is passed.If
markdowndb.config.js
is missing, it defaults to - no custom fields - and includes all files.If a config path is passed but doesn't exist, then it raises an error:
Error loading configuration file from...
.