{"id":20704374,"url":"https://github.com/cocoatoucher/styledmarkdown","last_synced_at":"2025-04-23T01:24:59.408Z","repository":{"id":37030929,"uuid":"444215309","full_name":"cocoatoucher/StyledMarkdown","owner":"cocoatoucher","description":"Generate SwiftUI Text or AttributedString from markdown strings with custom style names.","archived":false,"fork":false,"pushed_at":"2024-06-12T16:16:19.000Z","size":815,"stargazers_count":21,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-29T20:51:09.421Z","etag":null,"topics":["attributed","attributedstring","ios","localisation","localization","macos","markdown","string","style","styled","swift","swiftui","text","tvos","watchos"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cocoatoucher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-03T22:20:51.000Z","updated_at":"2025-03-13T16:04:37.000Z","dependencies_parsed_at":"2024-06-12T22:27:01.916Z","dependency_job_id":null,"html_url":"https://github.com/cocoatoucher/StyledMarkdown","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoatoucher%2FStyledMarkdown","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoatoucher%2FStyledMarkdown/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoatoucher%2FStyledMarkdown/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoatoucher%2FStyledMarkdown/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cocoatoucher","download_url":"https://codeload.github.com/cocoatoucher/StyledMarkdown/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250350294,"owners_count":21416107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["attributed","attributedstring","ios","localisation","localization","macos","markdown","string","style","styled","swift","swiftui","text","tvos","watchos"],"created_at":"2024-11-17T01:12:04.020Z","updated_at":"2025-04-23T01:24:59.384Z","avatar_url":"https://github.com/cocoatoucher.png","language":"Swift","readme":"\u003cp align=\"center\"\u003e\n\u003cimg src=\"Docs/logo.png\" width=\"300\" max-width=\"80%\" alt=\"glide\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\niOS 15.0 / macOS 12.0 / tvOS 15.0 / watchOS 8.0\n\u003c/p\u003e\n\niOS 15 comes with markdown string support. By default, you can style your strings with standard markdown syntax like **`**bold**`** and *`*italic*`*\nThis is nice, but there is usually a need to specify more complex styling like `font type`, `text color` etc.\n\n---\n\nStyledMarkdown is a mini library that lets you define custom styles in code and use those styles in your localized markdown strings.\n\n- Define your custom styles in code via specifying attributes like custom fonts, text colors, underline styles and more, and name those syles.\n- Use those style names in your markdown strings.\n- Provide your custom style definitions and markdown strings which are using those custom styles' names to StyledMarkdown. StyledMarkdown can create `SwiftUI` `Text` views or just `AttributedString`s for `UIKit`.\n\nWith this approach, you do not have to define a custom `AttributedStringKey` each time you want a custom style in your markdown, as StyledMarkdown handles this part for you.\n\n## Examples\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"Docs/examples.png\" width=\"400\" max-width=\"80%\" alt=\"glide devices\"/\u003e\n\u003c/p\u003e\n\n## Sample usage\n\n```swift\nlet normalStyle = Style { style in\n\tstyle.font = .subheadline\n\tstyle.foregroundColor = .red\n}\n\nlet boldStyle = Style { style in\n\tstyle.font = Font.italic(.system(size: 20))()\n\tstyle.foregroundColor = .blue\n}\n\nlet myStyleGroup = StyleGroup(\n\tbase: normalStyle,\n\t[\n\t\t\"bold\": boldStyle\n\t]\n)\n\nText(\n\t\"Hey ^[buddy](style: 'bold')\",\n\tstyleGroup: myStyleGroup\n)\n\n// or\n\nAttributedString(\n\tlocalized: \"Hey ^[buddy](style: 'bold')\",\n\tstyleGroup: myStyleGroup\n)\n```\n\n***The idea of StyleGroup and named Styles comes directly from [`SwiftRichString` library by `Daniele Margutti` on GitHub](https://github.com/malcommac/SwiftRichString). Some of the code from there is also used in this package.***\n\n## Supported modifiers\n\n#### font(*SwiftUI.Font* or *UIFont*)\n#### foregroundColor(*SwiftUI.Color* or *UIColor*)\n#### backgroundColor(*SwiftUI.Color* or *UIColor*)\n#### strikethrough(*SwiftUI.Color* or *UIColor*)\n#### strikethroughStyle(*NSUnderlineStyle*)\n#### underline(*SwiftUI.Color* or *UIColor*)\n#### underlineStyle(*NSUnderlineStyle*)\n#### kerning(*CGFloat*)\n#### tracking(*CGFloat*)\n#### baselineOffset(*CGFloat*)\n\n### Advanced styling\n\n```swift\nlet rainbow: [Color] = [\n\t.blue, .teal, .red, .gray, .yellow, .orange, .purple\n]\n\nlet rainbowStyleGroup = StyleGroup(\n\tstyleCustom: { source in\n\t\tvar attrString = source\n\t\tfor run in attrString.runs {\n\t\t\tlet currentRange = run.range\n\t\t\tvar index = currentRange.lowerBound\n\t\t\tvar colorCounter: Int = 0\n\t\t\twhile index \u003c currentRange.upperBound {\n\t\t\t\tlet nextIndex = attrString.characters.index(\n\t\t\t\t\tindex,\n\t\t\t\t\toffsetBy: 1\n\t\t\t\t\t)\n\t\t\t\tattrString[index ..\u003c nextIndex].foregroundColor = rainbow[colorCounter]\n\t\t\t\tcolorCounter += 1\n\t\t\t\tif colorCounter \u003e= rainbow.count {\n\t\t\t\t\tcolorCounter = 0\n\t\t\t\t}\n\t\t\t\tindex = nextIndex\n\t\t\t}\n\t\t}\n\t\treturn attrString\n\t}\n)\n\nText(\n\t\"Rainbow\",\n\tstyleGroup: rainbowStyleGroup\n)\n\n// or\n\nAttributedString(\n\tlocalized: \"Rainbow\",\n\tstyleGroup: rainbowStyleGroup\n)\n```\n_Parts of the above code for rainbow styling is taken from WWDC'21 sample app project called Caffe, Copyright © 2021 Apple Inc._\n\n### 🔗 Links\n\nYou can add links inside your strings using the custom `link` `AttributedStringKey`:\n`^[styled link](link: {url: 'http://www.example.com', style: 'linkStyle'})`\n\n### iOS Automatic grammar agreement\n\nAutomatic grammar agreement's `inflect` property works with StyledMarkdown styles.\n\n`^[2 salad](style: 'italic', inflect: true)`\n\ngenerates\n`2 salads` with italic style.\n\n### 🎆 Images (not supported)\n\niOS currently does not support including custom `Image` attachments within `AttributedString`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoatoucher%2Fstyledmarkdown","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcocoatoucher%2Fstyledmarkdown","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoatoucher%2Fstyledmarkdown/lists"}