Enabling Non-Latin Slugs in Ghost 5.89.1: A Step-by-Step Guide

Enabling Non-Latin Slugs in Ghost 5.89.1: A Step-by-Step Guide

In this guide, i'll address a common issue in Ghost version 5.89.1 where non-Latin characters in URLs are converted to their Latin equivalents. This behavior can be problematic for websites that use languages such as Korean, Japanese, Arabic, and others. By following the steps in this guide, you'll be able to modify Ghost so that it retains non-Latin characters in slugs, allowing for more accurate and culturally appropriate URLs.

Files to Modify

The following files need to be updated:

  • .versions\5.89.1\node_modules@tryghost\string\lib\slugify.js
  • .versions\5.89.1\core\built\admin\assets\ghost-<random-string>.js
  • .versions\5.89.1\node_modules@tryghost\validator\lib\validator.js

1. Modifying slugify.js

Original Code:

// Remove non-ascii characters
string = unidecode(string);

Updated Code:

// Remove non-ascii characters
// string = unidecode(string);

In this file, locate the line that removes non-ASCII characters using unidecode. By commenting out this line, you prevent the conversion of non-Latin characters into their closest Latin equivalents, allowing the original characters to be used directly in slugs.

2. Modifying ghost-<random-string>.js

Original Code:

const e=yield this.slugGenerator.generateSlug("post",t)
if(i=yield this.slugGenerator.generateSlug("post",t),i===r)

Updated Code:

const e=t
if(i=t,i===r)

This change ensures that slugs are assigned directly based on the input value (t), bypassing the internal slug generation process that could potentially alter the characters.

3. Modifying validator.js

Original Code:

validators.isSlug = function isSlug(str) {
    assertString(str);
    return validators.matches(str, /^[a-z0-9\-_]+$/);
};

Updated Code:

validators.isSlug = function isSlug(str) {
    assertString(str);
    return validators.matches(str, /^[a-z0-9\-_ㄱ-ㅎㅏ-ㅣ가-힣]+$/);
};

This modification expands the regular expression to include Korean characters, ensuring they are valid within slugs.

Supporting Other Non-Latin Languages

To support additional non-Latin languages, you can extend the regular expression by adding ranges for characters from other languages:

  • Arabic: \u0600-\u06FF
  • Cyrillic: \u0400-\u04FF
  • Greek: \u0370-\u03FF

Final Regular Expression Example:

validators.isSlug = function isSlug(str) {
    assertString(str);
    return validators.matches(str, /^[a-z0-9\-_ㄱ-ㅎㅏ-ㅣ가-힣\u0600-\u06FF\u0400-\u04FF\u0370-\u03FF]+$/);
};

This expanded regular expression now supports a variety of languages, making your Ghost site more versatile and user-friendly for a global audience.

Conclusion

After making these modifications, restart your Ghost instance to apply the changes. By following this guide, you will have successfully enabled the use of non-Latin characters in slugs, ensuring that your URLs can accurately reflect content in various languages without unnecessary conversion to Latin characters.

If you share this guide, please remember to credit the original source😊


Share Tweet Send
0 댓글들
Loading...
You've successfully subscribed to TMI
Great! Next, complete checkout for full access to TMI
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.