Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Unify all slugification under Slashlinks
Browse files Browse the repository at this point in the history
We had a couple subtle divergences from Slashlinks, since these regexes
were originally written to allow for a wider range of posix portable
file name characters.
  • Loading branch information
gordonbrander committed Oct 20, 2021
1 parent 8829554 commit 932f7d1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 126 deletions.
2 changes: 1 addition & 1 deletion xcode/Subconscious/Shared/Components/AppView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ enum AppAction {
AppAction.commit(
query: query,
// Since we don't have a slug, derive slug from query
slug: query.toSlug()
slug: Slashlink.slugify(query)
)
}
}
Expand Down
33 changes: 28 additions & 5 deletions xcode/Subconscious/Shared/Library/Slashlink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,40 @@ extension Slashlink {
}

/// Given a string, returns a slashlink slug *without* the slash prefix.
static func toSlug(_ text: String) -> String {
static func slugify(_ text: String) -> String {
text
.lowercased()
.replacingSpacesWithDash()
.removingNonPosixCharacters()
// Replace runs of one or more space with a single dash
.replacingOccurrences(
of: #"\s+"#,
with: "-",
options: .regularExpression,
range: nil
)
// Remove all non-slug characters
.replacingOccurrences(
of: #"[^a-zA-Z0-9_\-\/]"#,
with: "",
options: .regularExpression,
range: nil
)
.truncatingSafeFileNameLength()
.ltrim(prefix: "/")
}

/// Given a slug, returns a string that is close-enough to prose text
static func unslugify(_ slug: String) -> String {
slug
// Replace dash with space
.replacingOccurrences(
of: "-",
with: " "
)
.firstUppercased()
}

static func slashlinkToURLString(_ text: String) -> String? {
let slashlink = toSlug(text)
let slashlink = slugify(text)
if let url = URL(string: "sub://slashlink/\(slashlink)") {
return url.absoluteString
}
Expand All @@ -49,7 +72,7 @@ extension Slashlink {
name: String,
ext: String
) -> URL {
let slug = toSlug(name)
let slug = slugify(name)
let url = base.appendingFilename(name: slug, ext: ext)
if !FileManager.default.fileExists(atPath: url.path) {
return url
Expand Down
112 changes: 0 additions & 112 deletions xcode/Subconscious/Shared/Library/Slug.swift

This file was deleted.

16 changes: 16 additions & 0 deletions xcode/Subconscious/Shared/Library/StringUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ extension String {
)
}
}

extension String {
/// Truncate to avoid file name length limit issues.
/// Windows systems can handle up to 255, but we truncate at 200 to leave a bit of room
/// for things like version numbers.
func truncatingSafeFileNameLength() -> String {
String(self.prefix(200))
}
}

extension StringProtocol {
/// Upercase first letter in string.
func firstUppercased() -> String {
prefix(1).uppercased() + dropFirst()
}
}
2 changes: 1 addition & 1 deletion xcode/Subconscious/Shared/Models/Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Stub: Hashable, Identifiable {

init(title: String) {
self.title = title
self.slug = title.toSlug()
self.slug = Slashlink.slugify(title)
}

var id: String { slug }
Expand Down
3 changes: 2 additions & 1 deletion xcode/Subconscious/Shared/Services/DatabaseService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ struct DatabaseService {
})

var suggestions: [Suggestion] = []
if !resultSlugs.contains(query.toSlug()) {
let slug = Slashlink.slugify(query)
if !resultSlugs.contains(slug) {
suggestions.append(
.search(
Stub(title: query)
Expand Down
6 changes: 0 additions & 6 deletions xcode/Subconscious/Subconscious.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
B82C3A6326F576FC00833CC8 /* URLUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A6126F576FB00833CC8 /* URLUtilities.swift */; };
B82C3A6526F5796300833CC8 /* OptionalUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A6426F5796300833CC8 /* OptionalUtilities.swift */; };
B82C3A6626F5796300833CC8 /* OptionalUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A6426F5796300833CC8 /* OptionalUtilities.swift */; };
B82C3A6B26F6B18100833CC8 /* Slug.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A6A26F6B18100833CC8 /* Slug.swift */; };
B82C3A6C26F6B18100833CC8 /* Slug.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A6A26F6B18100833CC8 /* Slug.swift */; };
B82C3A6F26F6B1C000833CC8 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = B82C3A6E26F6B1C000833CC8 /* Collections */; };
B82C3A7126F6B1C000833CC8 /* OrderedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = B82C3A7026F6B1C000833CC8 /* OrderedCollections */; };
B82C3A7326F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B82C3A7226F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift */; };
Expand Down Expand Up @@ -113,7 +111,6 @@
B82C3A5E26F576C600833CC8 /* FileManagerUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileManagerUtilities.swift; sourceTree = "<group>"; };
B82C3A6126F576FB00833CC8 /* URLUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLUtilities.swift; sourceTree = "<group>"; };
B82C3A6426F5796300833CC8 /* OptionalUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptionalUtilities.swift; sourceTree = "<group>"; };
B82C3A6A26F6B18100833CC8 /* Slug.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Slug.swift; sourceTree = "<group>"; };
B82C3A7226F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OrdereredDictionaryUtilities.swift; sourceTree = "<group>"; };
B82C3A7526F6B5BF00833CC8 /* StringUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringUtilities.swift; sourceTree = "<group>"; };
B845FCC427051BF7001ECE4F /* Subtext4.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Subtext4.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -295,7 +292,6 @@
B82C3A7226F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift */,
B8EC568126F415C800AC64E5 /* PublisherBag.swift */,
B824FDD026FA98F300B81BBD /* ResultSet.swift */,
B82C3A6A26F6B18100833CC8 /* Slug.swift */,
B8EC568826F4204F00AC64E5 /* SQLite3Database.swift */,
B8EC568B26F4206400AC64E5 /* SQLite3Migrations.swift */,
B8082AA926F3B43A004B10BB /* Store.swift */,
Expand Down Expand Up @@ -513,7 +509,6 @@
B8EC568F26F4233B00AC64E5 /* AppEnvironment.swift in Sources */,
B82C3A7326F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift in Sources */,
B82C3A5326F528B000833CC8 /* DatabaseService.swift in Sources */,
B82C3A6B26F6B18100833CC8 /* Slug.swift in Sources */,
B878C9822703F47A0042DF72 /* SuggestionLabelView.swift in Sources */,
B8E4B6F226F28BA600B9BE21 /* GrowableTextViewRepresentable.swift in Sources */,
B8879EA026F90C5100A0B4FF /* AppNavigationView.swift in Sources */,
Expand Down Expand Up @@ -553,7 +548,6 @@
B8EC569026F4233B00AC64E5 /* AppEnvironment.swift in Sources */,
B82C3A7426F6B5B200833CC8 /* OrdereredDictionaryUtilities.swift in Sources */,
B82C3A5426F528B000833CC8 /* DatabaseService.swift in Sources */,
B82C3A6C26F6B18100833CC8 /* Slug.swift in Sources */,
B878C9832703F47A0042DF72 /* SuggestionLabelView.swift in Sources */,
B8E4B6F326F28BA700B9BE21 /* GrowableTextViewRepresentable.swift in Sources */,
B8879EA126F90C5100A0B4FF /* AppNavigationView.swift in Sources */,
Expand Down

0 comments on commit 932f7d1

Please sign in to comment.