W.D. Neumann's Thing

Fixing Section Casing

With a bit more playing around in Publish, I ran into another obstacle. I thought I'd try to put in a page for my CV. It'd give me a good spot to keep a current copy and it would help me when it comes time to futz with the CSS. So I add a case cv to my SectionID enum, rebuild and… ugh! Cv shows up in my section list. OK, I figure, it's probably pulling from the case's raw value, so I change it to case cv = "CV", rebuild it and… ugh! Cv still hows up in my section list.

I figure this is because the package is making use of Swift's capitalized String property, which, rather than just forcing the case of the first character to upper case, also forces all letters following the initial one to be lowercase.

For all I know there's some option somewhere to fix this, but who the hell has time to dig through all the source code for it? So I searched for "capitalized" and verified that, sure enough, that's what was going on. So to fix it, I added an additional processing step.

Alt-clicking on (or Cmd-clicking and looking at the definition of) publish in your main.swift shows that the full signature of publish is:

func publish(
  withTheme theme: Theme<Website>, 
  indentation: Indentation.Kind? = nil, 
  at path: Path? = nil, 
  rssFeedSections: Set<SectionID> = Set(SectionID.allCases), 
  rssFeedConfig: RSSFeedConfiguration? = .default, 
  deployedUsing deploymentMethod: DeploymentMethod<Website>? = nil, 
  additionalSteps: [PublishingStep<Website>] = [], 
  plugins: [Plugin<Website>] = [], 
  file: StaticString = #file
) throws -> PublishedWebsite<Website>

So we can see that additionalSteps takes an Array of PublishingSteps. Here's one area that Publish's README documentation does a good job of covering with its addDefaultSectionTitles() portion. A simple extension of PublishingStep allows us to change the casing of the "CV" section.

extension PublishingStep where Site == YourWebsiteStruct {
  static func fixSectionTitles() -> Self {
    .step(named: "Fix messed up section titles") { context in
      context.mutateAllSections { section in
        switch section.id {
        case .cv: section.title = "CV"
        default: break

Now we can change the call to publish to:

try Wdneumann().publish(
  withTheme: .wdnTheme, 
  additionalSteps: [.fixSectionTitles()], 
  plugins: [.splash(withClassPrefix: "")]

ow when we rebuild, the section name reads as "CV" rather than "Cv".

Up next, learning to Deploy the website.

Tagged with: