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

feat: Native emoji w/ image-based fallbacks and improved parsing #1746

Merged
merged 39 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
936d566
Render native emoji with image fallback
jhildenbiddle Feb 4, 2022
e7d627a
Deprecate emoji plugin
jhildenbiddle Feb 4, 2022
db1e94e
Add emoji tests
jhildenbiddle Feb 4, 2022
1fa45e5
Remove console.log statement
jhildenbiddle Feb 4, 2022
8252f3a
Fix emoji image alt attribute
jhildenbiddle Feb 4, 2022
b8c227f
Set nativeEmoji to false by default (non-breaking)
jhildenbiddle Feb 4, 2022
d03aa4f
Fix parsing emoji in HTML comments and script tags
jhildenbiddle Feb 4, 2022
39d5204
Add nativeEmoji and update noEmoji details
jhildenbiddle Feb 4, 2022
baf8044
Add Emoji plugin deprecation notice
jhildenbiddle Feb 5, 2022
39bf522
Fix ESLint issues
jhildenbiddle Feb 5, 2022
e1e8d22
Create build:emoji task
jhildenbiddle Feb 5, 2022
8c10b5f
Fix rendering of GitHub emoji without unicode
jhildenbiddle Feb 5, 2022
208049b
Adjust and match size of native and image emoji
jhildenbiddle Feb 5, 2022
b7a3b67
Update emoji test snapshot
jhildenbiddle Feb 5, 2022
260992c
Update docs test snapshot
jhildenbiddle Feb 5, 2022
2079441
Merge branch 'develop' into feat-native-emoji
Koooooo-7 Feb 5, 2022
8b587b8
Fix ci/codesandbox error
jhildenbiddle Feb 5, 2022
53d5fb5
Update native emoji font-stack
jhildenbiddle Feb 6, 2022
e9d5722
Fix rendering of native multi-character emoji
jhildenbiddle Feb 6, 2022
7c15fd7
Kick GitHub Workflow
jhildenbiddle Feb 6, 2022
05c7974
Merge branch 'develop' into feat-native-emoji
jhildenbiddle Mar 4, 2022
7ba8513
Replace rollup’s uglify plugin with terser
jhildenbiddle Mar 4, 2022
d52b476
Switch “npm ci” instead of “npm i” for stability
jhildenbiddle Mar 4, 2022
3f2dd46
Change emoji data from default to named export
jhildenbiddle Mar 4, 2022
e06fed4
Revert "Replace rollup’s uglify plugin with terser"
jhildenbiddle Mar 4, 2022
77cb0cd
Revert "Switch “npm ci” instead of “npm i” for stability"
jhildenbiddle Mar 4, 2022
98185ca
Revert "Change emoji data from default to named export"
jhildenbiddle Mar 4, 2022
bec3e0d
Specify codesandbox template and node version
jhildenbiddle Mar 4, 2022
16aec44
Update codesandbox config
jhildenbiddle Mar 4, 2022
27d4952
Revert "Revert "Replace rollup’s uglify plugin with terser""
jhildenbiddle Mar 4, 2022
4208c82
Revert "Revert "Revert "Replace rollup’s uglify plugin with terser"""
jhildenbiddle Mar 4, 2022
5120dd2
Update codesandbox config
jhildenbiddle Mar 4, 2022
8cdf27e
Revert "Update codesandbox config"
jhildenbiddle Mar 4, 2022
9d522d5
Fix codesandbox uglify error
jhildenbiddle Mar 4, 2022
ddf7ad2
Emoji docs tweaks
jhildenbiddle Mar 5, 2022
da6480d
Restore and update emoji plugin code
jhildenbiddle Mar 5, 2022
4a1d87e
Restore and update emoji plugin docs
jhildenbiddle Mar 5, 2022
d9fc279
Prettier updates
jhildenbiddle Mar 5, 2022
af45845
Match lowercase shortcodes only
jhildenbiddle Mar 5, 2022
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
3 changes: 2 additions & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"sandboxes": ["2d17z"],
"packages": [".", "packages/docsify-server-renderer"]
"packages": [".", "packages/docsify-server-renderer"],
"node": "16"
}
91 changes: 91 additions & 0 deletions build/emoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const axios = require('axios');
const fs = require('fs');
const path = require('path');

const filePaths = {
emojiMarkdown: path.resolve(process.cwd(), 'docs', 'emoji.md'),
emojiJS: path.resolve(
process.cwd(),
'src',
'core',
'render',
'emojify-data.js'
),
};

async function getEmojiData() {
const emojiDataURL = 'https://api.github.com/emojis';
const response = await axios.get(emojiDataURL);
const baseURL = Object.values(response.data)
.find(url => /unicode\//)
.split('unicode/')[0];
const data = { ...response.data };

// Remove base URL from emoji URLs
Object.entries(data).forEach(
([key, value]) => (data[key] = value.replace(baseURL, ''))
);
sy-records marked this conversation as resolved.
Show resolved Hide resolved

return {
baseURL,
data,
};
}

function writeEmojiPage(emojiData) {
const emojiPage =
(fs.existsSync(filePaths.emojiMarkdown) &&
fs.readFileSync(filePaths.emojiMarkdown, 'utf8')) ||
`<!-- START -->\n\n<!-- END -->`;
const emojiRegEx = /(<!--\s*START.*-->\n)([\s\S]*)(\n<!--\s*END.*-->)/;
const emojiMatch = emojiPage.match(emojiRegEx);
const emojiMarkdownStart = emojiMatch[1].trim();
const emojiMarkdown = emojiMatch[2].trim();
const emojiMarkdownEnd = emojiMatch[3].trim();
const newEmojiMarkdown = Object.keys(emojiData.data)
.reduce(
(preVal, curVal) =>
(preVal += `:${curVal}: ` + '`' + `:${curVal}:` + '`' + '\n\n'),
''
)
.trim();

if (emojiMarkdown !== newEmojiMarkdown) {
const newEmojiPage = emojiPage.replace(
emojiMatch[0],
`${emojiMarkdownStart}\n${newEmojiMarkdown}\n${emojiMarkdownEnd}`
);

fs.writeFileSync(filePaths.emojiMarkdown, newEmojiPage);
console.info(`- Created new file: ${filePaths.emojiMarkdown}`);
} else {
console.info(`- No changes to file: ${filePaths.emojiMarkdown}`);
}
}

function writeEmojiJS(emojiData) {
const emojiJS =
fs.existsSync(filePaths.emojiJS) &&
fs.readFileSync(filePaths.emojiJS, 'utf8');
const newEmojiJS = `export default ${JSON.stringify(emojiData, {}, 2)}`;

if (!emojiJS || emojiJS !== newEmojiJS) {
fs.writeFileSync(filePaths.emojiJS, newEmojiJS);
console.info(`- Created new file: ${filePaths.emojiJS}`);
} else {
console.info(`- No changes to file: ${filePaths.emojiJS}`);
}
}

(async () => {
console.log('Build emoji');

try {
const emojiData = await getEmojiData();

writeEmojiPage(emojiData);
writeEmojiJS(emojiData);
} catch (e) {
console.error(e);
}
})();
1 change: 1 addition & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Write a Plugin](write-a-plugin.md)
- [Markdown configuration](markdown.md)
- [Language highlighting](language-highlight.md)
- [Emoji](emoji.md)

- Guide

Expand Down
90 changes: 84 additions & 6 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The config can also be defined as a function, in which case the first argument i

```html
<script>
window.$docsify = function(vm) {
window.$docsify = function (vm) {
return {
markdown: {
renderer: {
Expand Down Expand Up @@ -319,14 +319,14 @@ window.$docsify = {
markdown: {
smartypants: true,
renderer: {
link: function() {
link: function () {
// ...
},
},
},

// function
markdown: function(marked, renderer) {
markdown: function (marked, renderer) {
// ...
return marked;
},
Expand Down Expand Up @@ -398,19 +398,97 @@ window.$docsify = {

Note that if you are running an external script, e.g. an embedded jsfiddle demo, make sure to include the [external-script](plugins.md?id=external-script) plugin.

## nativeEmoji

- type: `Boolean`
- default: `false`

Render emoji shorthand codes using GitHub-style emoji images or platform-native emoji characters.

```js
window.$docsify = {
nativeEmoji: true,
};
```

```markdown
:smile:
:partying_face:
:joy:
:+1:
:-1:
```

GitHub-style images when `false`:

<output data-lang="output">
<img class="emoji" src="https://github.githubassets.com/images/icons/emoji/unicode/1f604.png" alt="smile">
<img class="emoji" src="https://github.githubassets.com/images/icons/emoji/unicode/1f973.png" alt="partying_face">
<img class="emoji" src="https://github.githubassets.com/images/icons/emoji/unicode/1f602.png" alt="joy">
<img class="emoji" src="https://github.githubassets.com/images/icons/emoji/unicode/1f44d.png" alt="+1">
<img class="emoji" src="https://github.githubassets.com/images/icons/emoji/unicode/1f44e.png" alt="-1">
</output>

Platform-native characters when `true`:

<output data-lang="output">
<span class="emoji">😄︎</span>
<span class="emoji">🥳︎</span>
<span class="emoji">😂︎</span>
<span class="emoji">👍︎</span>
<span class="emoji">👎︎</span>
</output>

To render shorthand codes as text, replace `:` characters with the `&colon;` HTML entity.

```markdown
&colon;100&colon;
```

<output data-lang="output">

&colon;100&colon;

</output>

## noEmoji

- type: `Boolean`
- default: `false`

Disabled emoji parse.
Disabled emoji parsing and render all emoji shorthand as text.

```js
window.$docsify = {
noEmoji: true,
};
```

?> If this option is `false` but you don't want to emojify some specific colons, [refer to this](https://github.com/docsifyjs/docsify/issues/742#issuecomment-586313143)
```markdown
:100:
```

<output data-lang="output">

&colon;100&colon;

</output>

To disable emoji parsing of individual shorthand codes, replace `:` characters with the `&colon;` HTML entity.

```markdown
:100:

&colon;100&colon;
```

<output data-lang="output">

:100:

&colon;100&colon;

</output>

## mergeNavbar

Expand All @@ -435,7 +513,7 @@ See https://github.com/lukeed/tinydate#patterns
window.$docsify = {
formatUpdated: '{MM}/{DD} {HH}:{mm}',

formatUpdated: function(time) {
formatUpdated: function (time) {
// ...

return time;
Expand Down
Loading