Skip to content

Commit

Permalink
simon/i26 emoji helper functions (#56)
Browse files Browse the repository at this point in the history
* add emoji parsing and a few helper functions

* update changelog and readme

* wasm api demo and readme

* add two failing cases that I found while making the web demo

* also test if all desktop set emojis are counted as one emoji
currently fails on region flags

* fix region flags

* fix bug cause by faulty skin tone range

* cargo fmt

* fix fmt
  • Loading branch information
Simon-Laux authored May 7, 2024
1 parent bbf5061 commit f010384
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Unreleased

### Added
- Add new methods for working with emojis (they are standalone helper functions and not part of message parsing):
- `parser::is_emoji::emoji`(rust only) - nom parser that eats one emoji
- `parser::is_emoji::get_first_emoji(text)` - get first emoji if text begins with an emoji
- `parser::is_emoji::count_emojis_if_only_contains_emoji(text)` - counts emojis in texts that contain only emojis

## 0.9.0 - Improve BotCommandSuggestion Parsing

### Fixed
Expand Down
13 changes: 13 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,16 @@ docs about benchmarking: https://bheisler.github.io/criterion.rs/book/criterion_

- Older discussion on introducing markdown into deltachat: https://github.com/deltachat/interface/pull/20
- Feature request for message markdown in the forum: https://support.delta.chat/t/markdown-support-in-chat/159


## Emoji Helpers

Additionally to message parsing this crate also contains some useful functions for working with emojis.

- `parser::is_emoji::emoji` (rust only) - nom parser that eats one emoji
- idea: could potentially be used by core to filter reactions to only emojis
- `parser::is_emoji::get_first_emoji(text)` - get first emoji if text begins with an emoji
- idea: can be used by UI to get the first emoji of a chat name to display it as text avatar
- `parser::is_emoji::count_emojis_if_only_contains_emoji(text)` - counts emojis in texts that contain only emojis
- useful for jumbomoji logic (if you send a small message with just emojis the emojis get displayed larger).
- this function does not fail on too long strings, so to keep good performance check the length beforehand and if it is too long the message would not be big anyway so you don't need to call this function.
10 changes: 10 additions & 0 deletions message_parser_wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ For usage in react you can look at how we integrated this package in deltachat-d
If you want to see it in action in deltachat-desktop, feel free to download it on <https://get.delta.chat>.
### Emoji Helper functions
```js
/** returns first emoji from text if text begins with an emoji */
export function get_first_emoji(input: string): string | undefined;
/** If string contains only emojis count the emojis otherwise retuns null */
export function count_emojis_if_only_contains_emoji(input: string): number | undefined;
```

### For Devs

#### 🛠️ Build with `wasm-pack build`
Expand Down
36 changes: 34 additions & 2 deletions message_parser_wasm/example.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,26 @@
border-radius: 2px;
padding: 2px;
}

table,
td,
tr {
border: 1px solid grey;
border-collapse: collapse;
}
td {
padding: 3px;
}
</style>
<h1>Message Parser</h1>
<h3>Input</h3>
<textarea
name="content"
id="input"
style="width: calc(100% - 10px)"
rows="10"
>Sample message.
>
Sample message.
#sampleHashtag
https://example.com/example
[email protected]
Expand All @@ -59,7 +71,8 @@ <h3>Input</h3>
print("And, of course")
print("A code block")
```
</textarea>
</textarea
>
<label for="parse_mode">Mode: </label>
<select name="parse_mode" id="parse_mode">
<option value="text">TEXT Only</option>
Expand Down Expand Up @@ -88,6 +101,25 @@ <h3>Output - AST</h3>
border: 1px solid lightgray;
"
></pre>

<h1>Emoji Helpers</h1>

<table>
<thead>
<tr>
<td>Input</td>
<td>First Emoji</td>
<td>Emoji count if only emojis</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" id="emoji-test" /></td>
<td id="emoji-test-first"></td>
<td id="emoji-test-count"></td>
</tr>
</tbody>
</table>
<script type="module" src="example.js"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions message_parser_wasm/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import init, {
parse_desktop_set,
parse_text,
get_first_emoji,
count_emojis_if_only_contains_emoji,
} from "./pkg/message_parser_wasm.js";

/** @typedef {import("./pkg/message_parser_wasm.js").ParsedElement} ParsedElement */
Expand Down Expand Up @@ -169,4 +171,19 @@ init().then(() => {
localStorage.setItem("lastMode", parse_mode.value);
action();
};

// emoji helpers
/** @type {HTMLInputElement} */
const emoji_input = document.getElementById("emoji-test");
const emoji_out_first = document.getElementById("emoji-test-first");
const emoji_out_count = document.getElementById("emoji-test-count");
const emoji_update = () => {
const text = emoji_input.value;
emoji_out_first.innerText = String(get_first_emoji(text));
emoji_out_count.innerText = String(
count_emojis_if_only_contains_emoji(text)
);
setTimeout(emoji_update, 1)
};
emoji_input.onchange = emoji_input.onkeydown = ()=>setTimeout(emoji_update, 1);
});
12 changes: 12 additions & 0 deletions message_parser_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ export type ParsedElement =
c: { label: ParsedElement[]; destination: LinkDestination };
};
"#;

/// returns first emoji from text if text begins with an emoji
#[wasm_bindgen]
pub fn get_first_emoji(input: &str) -> Option<String> {
deltachat_message_parser::parser::is_emoji::get_first_emoji(input).map(|s| s.to_owned())
}

/// If string contains only emojis count the emojis otherwise retuns null
#[wasm_bindgen]
pub fn count_emojis_if_only_contains_emoji(input: &str) -> Option<u32> {
deltachat_message_parser::parser::is_emoji::count_emojis_if_only_contains_emoji(input)
}
Loading

0 comments on commit f010384

Please sign in to comment.