Skip to content

Commit a527706

Browse files
author
Bryce Kalow
authored
fix: update config value to partialsDirectories (#231)
1 parent ca69d24 commit a527706

File tree

9 files changed

+56
-14
lines changed

9 files changed

+56
-14
lines changed

.changeset/serious-pants-lie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hashicorp/platform-content-conformance': patch
3+
---
4+
5+
Update partialsDirectory to partialsDirectories and add docs/partials

packages/content-conformance/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Rules can be specified with differing severity to control the check's failure st
6969
export default {
7070
root: '.',
7171
contentFileGlobPattern: 'content/**/*.mdx',
72-
partialsDirectory: 'content/partials',
72+
partialsDirectories: ['content/partials'],
7373
rules: {
7474
'with-config': [
7575
'error',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am a partial file

packages/content-conformance/src/__tests__/config.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ describe('loadConfig', () => {
1818
"./rules/must-have-h1": "error",
1919
}
2020
`)
21-
expect(config.partialsDirectory).toEqual('content/partials')
21+
expect(config.partialsDirectories).toEqual([
22+
'content/partials',
23+
'docs/partials',
24+
])
2225
})
2326

2427
test('it loads config presets', async () => {

packages/content-conformance/src/__tests__/content-file.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ this is an invalid multi-line description.
126126
`)
127127
})
128128

129-
it('should set isPartial based on the path and partialsDirectory', () => {
129+
it('should set isPartial based on the path and partialsDirectories', () => {
130130
const file = new ContentFile(
131131
{ value: 'I am a partial', path: 'content/partials/partial.mdx' },
132-
{ partialsDirectory: 'content/partials' }
132+
{ partialsDirectories: ['content/partials'] }
133133
)
134134

135135
expect(file.isPartial).toBeTruthy()

packages/content-conformance/src/__tests__/engine.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,37 @@ describe('ContentConformanceEngine', () => {
144144
}
145145
`)
146146
})
147+
148+
test('detects partials in docs/partials', async () => {
149+
const filePaths: Set<ContentFile> = new Set()
150+
const opts = {
151+
root: getFixturePath('basic-with-content-files'),
152+
contentFileGlobPattern: '{content,docs}/**/*.mdx',
153+
partialsDirectories: ['docs/partials'],
154+
rules: [
155+
{
156+
level: 'warn' as const,
157+
type: 'content' as const,
158+
id: 'fake-rule-for-visiting-all-files',
159+
description: 'This is a fake rule for testing purposes',
160+
executor: {
161+
async contentFile(file) {
162+
// accumulate files for assertion
163+
if (file.isPartial) filePaths.add(file.path)
164+
},
165+
},
166+
},
167+
],
168+
}
169+
170+
const engine = new ContentConformanceEngine(opts)
171+
172+
await engine.execute()
173+
174+
expect(filePaths).toMatchInlineSnapshot(`
175+
Set {
176+
"docs/partials/some-partial.mdx",
177+
}
178+
`)
179+
})
147180
})

packages/content-conformance/src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { loadModuleFromFilePath, getPackageFilePath } from './utils.js'
77
const CONFIG_FILE_NAME = 'content-conformance.config.mjs'
88

99
const CONFIG_DEFAULTS = {
10-
partialsDirectory: 'content/partials',
10+
partialsDirectories: ['content/partials', 'docs/partials'],
1111
}
1212

1313
const RuleLevels = z.enum(['off', 'warn', 'error'])
@@ -22,7 +22,7 @@ const ContentConformanceConfig = z.object({
2222
root: z.string(),
2323
preset: z.string().optional(),
2424
contentFileGlobPattern: z.string(),
25-
partialsDirectory: z.string().optional(),
25+
partialsDirectories: z.array(z.string()).optional(),
2626
dataFileGlobPattern: z.string().optional(),
2727
presets: z.array(z.string()).optional(),
2828
rules: z.record(ContentConformanceConfigRule).optional(),
@@ -88,8 +88,8 @@ export async function loadConfig({
8888
}
8989

9090
function applyConfigDefaults(config: ContentConformanceConfig) {
91-
if (!config.partialsDirectory) {
92-
config.partialsDirectory = CONFIG_DEFAULTS.partialsDirectory
91+
if (!config.partialsDirectories) {
92+
config.partialsDirectories = [...CONFIG_DEFAULTS.partialsDirectories]
9393
}
9494
}
9595

packages/content-conformance/src/content-file.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Test } from 'unist-util-is'
1111
import type { Node } from 'unist'
1212

1313
interface ContentFileOpts {
14-
partialsDirectory?: string
14+
partialsDirectories?: string[]
1515
}
1616

1717
/**
@@ -34,10 +34,10 @@ export class ContentFile extends VFile {
3434
super(value)
3535
this.parseFrontmatter()
3636

37-
// If a partialsDirectory is provided, check if the file is in the directory
38-
if ((value as Options)?.path && opts?.partialsDirectory) {
39-
this.isPartial = String((value as Options).path).startsWith(
40-
opts.partialsDirectory
37+
// If a partialsDirectories is provided, check if the file is in the directory
38+
if ((value as Options)?.path && opts?.partialsDirectories) {
39+
this.isPartial = opts.partialsDirectories.some((partialsDirectory) =>
40+
String((value as Options).path).startsWith(partialsDirectory)
4141
)
4242
}
4343
}

packages/content-conformance/src/engine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class ContentConformanceEngine {
7373
path: String(filepath),
7474
value: contents,
7575
},
76-
{ partialsDirectory: this.opts.partialsDirectory }
76+
{ partialsDirectories: this.opts.partialsDirectories }
7777
)
7878

7979
this.contentFiles.push(file)

0 commit comments

Comments
 (0)