inCode (xs:ys)
  | lastElm ≡ Just ':' = endCodeLines ⧺ ["#### " ⊕ init xs' ⊕ " ####", "```"] ⧺ modifyLines True ys
  | all isSpace' xs = endCodeLines ⧺ [""] ⧺ modifyLines False ys
  | otherwise = [drop 2 xs] ⧺ modifyLines inCode ys
  where
    xs' = dropWhileEnd isSpace' (dropWhile isSpace' xs)
    isSpace' = liftA2 (∨) isSpace (≡'\b')
    lastElm = if null xs' then Nothing else Just (last xs')
    endCodeLines = bool [] ["```"] inCode

addSubTitles ∷ [String] → [Str
modifyLines inCode (xs:ys)
  | lastElm ≡ Just ':' = endCodeLines ⧺ ["#### " ⊕ init xs' ⊕ " ####", "```"] ⧺ modifyLines True ys
  | all isSpace' xs = endCodeLines ⧺ [""] ⧺ modifyLines False ys
  | otherwise = [drop 2 xs] ⧺ modifyLines inCode ys
  where
    xs' = dropWhileEnd isSpace' (dropWhile isSpace' xs)
    isSpace' = liftA2 (∨) isSpace (≡'\b')
    lastElm = if null xs' then Nothing else Just (last xs')
    endCodeLines = bool [] ["```"] inCode
modifyLines inCode (xs:ys)
  | lastElm ≡ Just ':' = endCodeLines ⧺ ["#### " ⊕ init xs' ⊕ " ####", "```"] ⧺ modifyLines True ys
  | all isSpace' xs = endCodeLines ⧺ [""] ⧺ modifyLines False ys
  | otherwise = [drop 2 xs] ⧺ modifyLines inCode ys
  where
    xs' = dropWhileEnd isSpace' (dropWhile isSpace' xs)
    isSpace' = liftA2 (∨) isSpace (≡'\b')
    lastElm = if null xs' then Nothing else Just (last xs')
    endCodeLines = bool [] ["```"] inCode