fix(publish): changedPostDirs normalisiert ../-präfix im contentRoot

This commit is contained in:
Jörg Lohrer 2026-04-21 10:10:01 +02:00
parent 367af9df9f
commit b89442bf5c
2 changed files with 30 additions and 1 deletions

View File

@ -4,6 +4,10 @@ function escapeRegex(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
} }
function stripParentSegments(path: string): string {
return path.replace(/^(\.\.\/)+/, '')
}
export function filterPostDirs(lines: string[], contentRoot: string): string[] { export function filterPostDirs(lines: string[], contentRoot: string): string[] {
const root = contentRoot.replace(/\/$/, '') const root = contentRoot.replace(/\/$/, '')
const prefix = root + '/' const prefix = root + '/'
@ -49,7 +53,8 @@ export interface DiffArgs {
export async function changedPostDirs(args: DiffArgs): Promise<string[]> { export async function changedPostDirs(args: DiffArgs): Promise<string[]> {
const runner = args.runner ?? defaultRunner const runner = args.runner ?? defaultRunner
const stdout = await runner(['diff', '--name-only', `${args.from}..${args.to}`]) const stdout = await runner(['diff', '--name-only', `${args.from}..${args.to}`])
return filterPostDirs(stdout.split('\n'), args.contentRoot) const normalizedRoot = stripParentSegments(args.contentRoot)
return filterPostDirs(stdout.split('\n'), normalizedRoot)
} }
export async function allPostDirs(contentRoot: string): Promise<string[]> { export async function allPostDirs(contentRoot: string): Promise<string[]> {

View File

@ -85,6 +85,30 @@ Deno.test('filterPostDirs: _drafts unter sprach-ebene wird ignoriert', () => {
assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/real']) assertEquals(filterPostDirs(lines, 'content/posts'), ['content/posts/de/real'])
}) })
Deno.test('changedPostDirs: normalisiert contentRoot mit ../-präfix', async () => {
const runner: GitRunner = () =>
Promise.resolve('content/posts/de/alpha/index.md\ncontent/posts/en/beta/index.md\n')
const dirs = await changedPostDirs({
from: 'HEAD~1',
to: 'HEAD',
contentRoot: '../content/posts',
runner,
})
assertEquals(dirs.sort(), ['content/posts/de/alpha', 'content/posts/en/beta'])
})
Deno.test('changedPostDirs: normalisiert contentRoot mit mehrfachem ../-präfix', async () => {
const runner: GitRunner = () =>
Promise.resolve('content/posts/de/alpha/index.md\n')
const dirs = await changedPostDirs({
from: 'HEAD~1',
to: 'HEAD',
contentRoot: '../../content/posts',
runner,
})
assertEquals(dirs, ['content/posts/de/alpha'])
})
Deno.test('allPostDirs: findet posts in sprach-unterordnern', async () => { Deno.test('allPostDirs: findet posts in sprach-unterordnern', async () => {
const tmp = await Deno.makeTempDir() const tmp = await Deno.makeTempDir()
try { try {