Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inline rules #114

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- markymark (10.1.2)
- markymark (10.1.3)
- SwiftLint (0.28.1)

DEPENDENCIES:
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
markymark: 98903145fd4c412c9b7836c6a40e427652e8cca2
markymark: 264a58bb797251c4d0e46865aa5ce78d210d5d2a
SwiftLint: 7f5f7de0da74a649b16616cb5246ae323489656e

PODFILE CHECKSUM: e6179d5e64bda0057471cea1521ff93bf207a88b
Expand Down
63 changes: 44 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ https://github.com/M2Mobi/Marky-Mark
## Simple usage

### View with default styling

```swift
let markDownView = MarkDownTextView()
markDownView.text = "# Header\nParagraph"
```

MarkDownTextView can be used with two configurations, either `view` or `attributedString`. In both cases the paragraphs will be rendered as `attributedString`, but when using the `view` configuration each 'block' (each new line in the markdown text is considered a block) will be rendered as view. The `view` option is recommended for better layout and flexibility but offers slightly less performance than the `attributedString` option. The default configuration is `view`.


```swift
let markDownView = MarkDownTextView(markDownConfiguration: .view)
```

```swift
let markDownView = MarkDownTextView(markDownConfiguration: .attributedString)
```

### View with modified styling

Markymark has many styling options, please check the examples in the styling section of this readme. A simple example:
Expand All @@ -54,8 +66,8 @@ markDownView.styling.paragraphStyling.baseFont = .systemFont(ofSize: 14)
markDownView.text = "# Header\nParagraph"
```


## Supported tags in the Default Flavor

Note: Different tags can be supported by either extending the ContentfulFlavor (default) or by implementing a class that comforms to `Flavor` and implement the required `Rule`'s

```
Expand Down Expand Up @@ -97,15 +109,16 @@ Code
\```code```
```


### Customizing default style

Default Styling instance

```swift
var styling = DefaultStyling()
```

#### Paragraphs (regular text)

Markdown example: `Some text`

```swift
Expand Down Expand Up @@ -147,6 +160,7 @@ styling.headingStyling.textAlignment = .left
```

#### linkStyling

Markdown Example `[Google](http://www.google.com)`

```swift
Expand All @@ -159,6 +173,7 @@ styling.linkStyling.isUnderlined = true
```

#### List styling

Markdown Example:

```
Expand Down Expand Up @@ -214,6 +229,7 @@ _Please check the `DefaultStyling` class for more information_


## Advanced usage

Advanced usage is only needed for very specific cases. Making subsets of styling, making different styling combinations, supporting different Markdown rules (syntax) or modifying certain views after that have been generated.

### Custom styling objects
Expand Down Expand Up @@ -250,38 +266,43 @@ MarkDownTextView(styling: CustomMarkyMarkStyling())
```

### Adding your own rules

Adding a new rule requires three new classes of based on the following protocol:

* `Rule` that can recoginizes the desired markdown syntax
* `Rule` or `InlineRule` that can recoginizes the desired markdown syntax
* `MarkDownItem` for your new element that will be created by your new rule
* `LayoutBlockBuilder` that can convert your MarkDownItem to layout

Add the rule to MarkyMark
To add a 'block' rule such as a Paragraph, List or Code Block:

```swift
markyMark.addRule(MyCustomRule())
markdownTextView.add(rule: MyCustomRule())
```

Or when using the MarkdownTextView:
To add an 'inline' rule such as a Bold, Italic, Link:

```swift
markdownTextView.add(rule: MyCustomRule())
markdownTextView.add(inlineRule: MyCustomInlineRule())
```

Add the block builder to your layout converter
Use either options to add a layout builder for a 'block' MarkDownItem created by a 'block' `Rule` (depending on the configuration view or attributedString):

For view:

```swift
converter.addLayoutBlockBuilder(MyCustomLayoutBlockBuilder())
markdownTextView.addViewLayoutBlockBuilder(MyCustomLayoutBlockBuilder())
```

Or when using the MarkdownTextView use either of these options (depending on the configuration view or attributedString):
For attributedString:

```swift
markdownTextView.addViewLayoutBlockBuilder(MyCustomLayoutBlockBuilder())
markdownTextView.addAttributedStringLayoutBlockBuilder(MyCustomLayoutBlockBuilder())
```

To add a layout builder for an inline item (MarkdownItem to attributedString) created be a `InlineRule`:

```swift
markdownTextView.addAttributedStringLayoutBlockBuilder(MyCustomLayoutBlockBuilder())
markdownTextView.addInlineLayoutBlockBuilder(MyCustomInlineLayoutBlockBuilder())
```

If needed you can also add a custom styling class to the default styling
Expand All @@ -291,27 +312,31 @@ styling.addStyling(MyCustomStyling())
```

### Converter hook
The converter has a callback method which is called every time a `MarkDownItem` is converted to layout.
The converter has a callback method which is called every time a `MarkDownItem` is converted to view.

For view configuration:

```swift
converter.didConvertElement = {
markDownItem, view in
// Do something with markDownItem and / or view here
markDownTextView.onDidConvertMarkDownItemToView = {
markDownItem, view in

}
```

When using the MarkdownTextView
or when using attributedString configuration:

```swift
markDownTextView.onDidConvertMarkDownItemToView = {
markDownItem, view in
markDownTextView.onDidConvertMarkDownItemToAttributedString = {
markDownItem, string in

}
```

### Link behavior
By default Markymark opens URL's using `UIApplication.shared.delegate.open(_:open:options)`. links will only be openened when this method is implemented. Markymark allows changing this behavior by passing a custom URLOpener, an object that conforms to the `URLOpener` protocol.

URLOpener can only be used with `view` configuration.

```swift
let markDownView = MarkDownTextView()
markDownTextView?.urlOpener = MyCustomerURLOpener()
Expand Down
Loading