NoteRenamer fetches inbound graph edges, creates the note under the new slug (patching the topic frontmatter field if the prefix changes), rewrites all wikilinks and markdown links in linking notes, then deletes the old slug. Tests cover wikilink/markdown-link replacement and topic patching.
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System.Management.Automation;
|
|
using NozCli.Core.Client;
|
|
|
|
namespace Noz.PowerShell.Cmdlets;
|
|
|
|
[Cmdlet(VerbsCommon.Rename, "NozNote", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
|
|
[OutputType(typeof(NozNote))]
|
|
public sealed class RenameNozNoteCmdlet : NozCmdletBase
|
|
{
|
|
[Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
|
|
public string Slug { get; set; } = "";
|
|
|
|
[Parameter(Mandatory = true, Position = 1)]
|
|
public string NewSlug { get; set; } = "";
|
|
|
|
[Parameter]
|
|
public SwitchParameter PassThru { get; set; }
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
if (Slug == NewSlug)
|
|
{
|
|
WriteWarning("Quelle und Ziel sind identisch — nichts zu tun.");
|
|
return;
|
|
}
|
|
|
|
if (!ShouldProcess(Slug, $"Rename-NozNote → {NewSlug}")) return;
|
|
|
|
var steps = new List<string>();
|
|
|
|
var result = Run(NoteRenamer.ExecuteAsync(
|
|
Api, Slug, NewSlug,
|
|
onProgress: msg => WriteVerbose(msg)));
|
|
|
|
WriteVerbose($"Fertig: {result.LinksUpdated} Link(s) aktualisiert.");
|
|
|
|
if (result.LinksUpdated > 0)
|
|
{
|
|
foreach (var updated in result.UpdatedNotes)
|
|
WriteVerbose($" • {updated}");
|
|
}
|
|
|
|
if (PassThru)
|
|
{
|
|
var notes = Run(Api.GetNotesAsync());
|
|
var renamed = notes.Find(n => n.Slug == NewSlug);
|
|
if (renamed is not null) WriteObject(NozNote.From(renamed));
|
|
}
|
|
}
|
|
}
|