Cracking the Code

Inside Medium’s World-Class Editor

Jordan Scales
friendship .js
Published in
4 min readNov 4, 2014

--

Big websites are often seen as great mysteries to the common web developer. With technologies changing every week, and new updates to Ember releasing once every few years, it can be hard to keep up. Such changes prompt us to ask some of technology’s toughest questions: How do they scale this layout? How does this page load so quickly? How do they make money? No website fits this bill quite like Medium.com.

But for the expert web developer, as you will soon become, discovering the world behind the webpage is only a few keystrokes away.

Meet your new friend, Chrome DevTools. If you have Chrome, you already have DevTools installed. If you don’t have Chrome, you can download it for free at the following link: http://mac.softpedia.com/get/Internet-Utilities/Google-Chrome.shtml.

Now that you have chrome, go ahead and press the following key combination: ⌥⌘I. Please note that this key combination is only available on a Mac. If you don’t have a Mac, you can get one for free at the following link: http://www.freemacbookpro.com.

You should see a scary window underneath the current webpage. This is your pathway to developer nirvana — the Chrome Web Inspector. Here we can bounce around the webpage, view in detail the layout of all the HTML elements, and even change the styles of the page.

Let’s use this technique to check out one of our favorite websites.

Taking a closer look at the <body> tag, we see the following:

The <script> tags at the bottom tell us that Medium consists of three main components: jQuery, the editor, and tooltips.

We can dive in even further now, by checking out the source code of the webpage — the code that makes Medium work™. Clicking the “Sources” tab should show all of the JavaScript running on Medium. From here, click through to “editor.js” and see the following source.

Wow, okay, this is awkward. So this code is not very easy to read, so we can’t exactly run through it. Instead, let’s do what every junior web developer would do, and implement the Medium editor ourselves.

We’ll start off by making a <div> with the “id” field set to “editor.”

Next we’ll use some new JavaScript features to turn this <div> into a <work of art>. This will enable us to use many of the features Medium uses to create one of the best writing experiences on the web™.

And, that’s it! Congratulations, you’ve made yourself a basic Medium editor. You may be thinking, “Why is our JavaScript so compact while Medium’s is so long and verbose? By the way, are you single?” To answer your first question, it’s because our editor does not quite have all the features of the Medium editor. For example, double-clicking on a word on Medium brings up the following:

This is what’s known as a “tooltip” and Medium’s tooltips are very good. To write our own tooltips, we’ll start by using a library I wrote in anticipation for this post. You can download it for free from this link.

Then just include it in your document and create a new tooltip with the following JavaScript:

And here we have the last piece of the front-end puzzle.

We can see how using DevTools enables us to copy some of the most prestigious real estate on the web. A whole world of opportunity now lies at our fingertips. Behind every website are key design decisions and engineering trade-offs that you don’t need to make yourself, so go forth with your newly-found DevTools knowledge, and discover the hidden secrets behind some of your favorite websites.

Don’t forget to write.

Jordan Scales is a web developer and it has been three weeks since his last incident.

--

--

Jordan Scales
friendship .js

JavaScript clickbait enthusiast. Giving you superpowers.