未加星标

Some of My JS Tricks to Enhance the HTML Output of Markdown

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Just because Markdown is so simple (well, “simple”), there will definitely be useful features missing. I want to share some of my relatively simple JS tricks to fill the gaps. Please note that I’m only talking about the HTML output, and the Markdown flavor can be arbitrary (it doesn’t have to be Pandoc’s Markdown). In fact, all tricks below are general-purpose and will work on any web pages, no matter if the pages are generated from Markdown or not.

How to work with MathJax

The MathJax support in most Markdown flavors is missing. The only exception is Pandoc’s Markdown. If you use R Markdown (which is based on Pandoc), you can ignore this trick. I have explained my trick of making MathJax work with any flavors of Markdown in detailin a previous post. The relevant JS code is in math-code.js on my website. This is the most complicated trick in this post, although it is only 20 lines of code.

How to make links open in new tabs (i.e., target="_blank" ) Most Markdown renderers translate [text](url) to <a href="url">text</a> , which means links will be opened in the current tab or window. I have explained my trick of making links open in new tabs in a previous post, too. How to generate tooltips for images

Images on the web often have an alt attribute, which stands for “alternate text”. This is often invisible to readers without vision problems, but important for users who have to use screen readers (such as blind people). Without the alt text, they will have no idea what an image means. The text is also displayed with the image cannot be loaded.

Since this attribute is invisible to users with the normal vision, it actually also has a problem. Sometimes you don’t know what exactly the author tries to express just by looking at an image. You may have a better idea by reading the text. You could right click on the image, “Inspect Element”, and look at the <img> tag in the source, but that’s a bit hassle and not everyone knows how the use the Developer Tools of modern web browsers.

My trick is to add the title attribute to <img> (if not already specified), whose value is identical to the alt text. The title attribute will give you a tooltip, which you can read as you mouseover the image. XKCD is a typical example. The code alt-title.js is simple enough:

(function() { var img, imgs = document.getElementsByTagName('img'); for (var i = 0; i < imgs.length; i++) { img = imgs[i]; if (!img.title) img.title = img.alt; } })();

An example:


Some of My JS Tricks to Enhance the HTML Output of Markdown
Note that with Pandoc’s Markdown, you can generate figure captions, so the tooltips may not be necessary. With other Markdown flavors, ![text](url) is often translated to <img src="url" alt="text" /> . How to center images in paragraphs

When an image ( <img> ) is the only child element of a paragraph ( <p> ), chances are you want to center it. Except with Pandoc’s Markdown, you cannot really align images with Markdown syntax. I have written a JS script center-img.js to automatically center images on web pages. It is slightly tricky because I considered both of these cases:

<!-- Markdown: ![](...) --> <p> <img src="..." /> </p> <!-- Markdown: [![](...)](...) --> <p> <a href="..."><img src="..." /></a> </p>

I have also considered centering <embed> and <object> , although these elements are rarer in Markdown’s output (no native Markdown syntax for them).

How to right-align quote footers

If you need to use quotes in Markdown, everything will be left-aligned by default, but you may want the footer (the source of the quote or the person who said the words) to be right-aligned.

My trick, right-quote.js , is to check the last paragraph of a quote. If it starts with the em-dash (―), I’ll apply the CSS style text-aligh: right; to it. Here is an example:

> UNIX was not designed to stop its users from doing stupid things, > as that would also stop them from doing clever things. > --- Doug Gwyn

Output:

UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things. ― Doug Gwyn

I’m not a javascript expert. My code above may be naive and inefficient. I have found JavaScript a lot of fun to play with, even only with selectors like document.getElementsByTagName() or document.querySelectorAll() and simple for-loops. I used to think I couldn’t live without fancy libraries like jQuery, but later realized vanilla JavaScript could actually do a lot of things. I hope these tricks could be useful to web amateurs like me.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Some of My JS Tricks to Enhance the HTML Output of Markdown
本站链接:https://www.codesec.net/view/611806.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(11)