Markdown Markup Language



Working with Markdown files in Visual Studio Code is simple, straightforward, and fun. Besides VS Code's basic editing, there are a number of Markdown specific features that will help you be more productive.

  1. Markdown Markup Languages
  2. Markdown Vs Markup Language
  3. Markdown Markup Language Examples
  4. Markdown Formatting
  5. What Is A Markdown
  6. Markdown Markup Language Difference
  7. Markup Vs Markdown

I'm writing a java webapp and I'd like to let my users enter text containing markup that is XSS / Cross-Site-Scripting safe. I'd like to save the user generated markup to a database and display it as. Just like HTML or LaTeX, Markdown is a markup language. In contrast to these examples, however, Markdown aims to be as easy as possible for people to read. Each markup element is closely related to the actual meaning, rather than being abstract. Markdown is a easy-to-read and easy-to-write markup language in a plain text format. Originally writ t en as a Perl script by John Gruber and Aaron Swartz in 2004 as a way to write in an easy-to-read format that can be converted into HTML (or XHTML).

Markdown extensions

In addition to the functionality VS Code provides out of the box, you can install an extension for greater functionality.

Tip: Click on an extension tile above to read the description and reviews to decide which extension is best for you. See more in the Marketplace.

Markdown preview

VS Code supports Markdown files out of the box. You just start writing Markdown text, save the file with the .md extension and then you can toggle the visualization of the editor between the code and the preview of the Markdown file; obviously, you can also open an existing Markdown file and start working with it. To switch between views, press ⇧⌘V (Windows, Linux Ctrl+Shift+V) in the editor. You can view the preview side-by-side (⌘K V (Windows, Linux Ctrl+K V)) with the file you are editing and see changes reflected in real-time as you edit.

Here is an example with a very simple file.

Tip: You can also right-click on the editor Tab and select Open Preview (⇧⌘V (Windows, Linux Ctrl+Shift+V)) or use the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) to run the Markdown: Open Preview to the Side command (⌘K V (Windows, Linux Ctrl+K V)).

Markdown

Dynamic previews and preview locking

By default, Markdown previews automatically update to preview the currently active Markdown file:

You can lock a Markdown preview using the Markdown: Toggle Preview Locking command to keep it locked to its current Markdown document. Locked previews are indicated by [Preview] in the title:

Editor and preview synchronization

VS Code automatically synchronizes the Markdown editor and the preview panes. Scroll the Markdown preview and the editor is scrolled to match the preview's viewport. Scroll the Markdown editor and the preview is scrolled to match its viewport:

You can disable scroll synchronization using the markdown.preview.scrollPreviewWithEditor and markdown.preview.scrollEditorWithPreviewsettings.

The currently selected line in the editor is indicated in the Markdown preview by a light gray bar in the left margin:

Additionally, double clicking an element in the Markdown preview will automatically open the editor for the file and scroll to the line nearest the clicked element.

Outline view

The Outline view is a separate section in the bottom of the File Explorer. When expanded, it will show the symbol tree of the currently active editor. For Markdown files, the symbol tree is the Markdown file's header hierarchy.

The Outline view is a great way to review your document's header structure and outline.

Extending the Markdown preview

Extensions can contribute custom styles and scripts to the Markdown preview to change its appearance and add new functionality. Here's a set of example extensions that customize the preview:

Using your own CSS

You can also use your own CSS in the Markdown preview with the 'markdown.styles': []setting. This lists URLs for style sheets to load in the Markdown preview. These stylesheets can either be https URLs, or relative paths to local files in the current workspace.

For example, to load a stylesheet called Style.css at the root of your current workspace, use File > Preferences > Settings to bring up the workspace settings.json file and make this update:

Keep trailing whitespace in order to create line breaks

Markdown

To create hard line breaks, Markdown requires two or more spaces at the end of a line. Depending on your user or workspace settings, VS Code may be configured to remove trailing whitespace. In order to keep trailing whitespace in Markdown files only, you can add these lines to your settings.json:

Markdown preview security

For security reasons, VS Code restricts the content displayed in the Markdown preview. This includes disabling script execution and only allowing resources to be loaded over https.

When the Markdown preview blocks content on a page, an alert popup is shown in the top right corner of the preview window:

You can change what content is allowed in the Markdown preview by clicking on this popup or running the Markdown: Change preview security settings command in any Markdown file:

The Markdown preview security settings apply to all files in the workspace.

Here are the details about each of these security levels:

Strict

This is the default setting. Only loads trusted content and disables script execution. Blocks http images.

It is strongly recommended that you keep Strict security enabled unless you have a very good reason to change it AND you trust all markdown files in the workspace.

Allow insecure content

Keeps scripts disabled but allows content to be loaded over http.

Disable

Disables additional security in the preview window. This allows script execution and also allows content to be loaded over http.

Snippets for Markdown

There are several built-in Markdown snippets included in VS Code - press ⌃Space (Windows, Linux Ctrl+Space) (Trigger Suggest) and you get a context specific list of suggestions.

Tip: You can add in your own User Defined Snippets for Markdown. Take a look at User Defined Snippets to find out how.

Compiling Markdown into HTML

VS Code integrates with Markdown compilers through the integrated task runner. We can use this to compile .md files into .html files. Let's walk through compiling a simple Markdown document.

Step 1: Install a Markdown compiler

For this walkthrough, we use the popular Node.js module, markdown-it.

Note: There are many Markdown compilers to choose from beyond markdown-it. Pick the one that best suits your needs and environment.

Step 2: Create a simple MD file

Open VS Code on an empty folder and create a sample.md file.

Note: You can open a folder with VS Code by either selecting the folder with File > Open Folder or navigating to the folder and typing 'code .' at the command line.

Place the following source code in that file:

Step 3: Create tasks.json

The next step is to set up the task configuration file tasks.json. To do this, run Terminal > Configure Tasks and click Create tasks.json file from templates. VS Code then presents a list of possible tasks.json templates to choose from. Select Others since we want to run an external command.

This generates a tasks.json file in your workspace .vscode folder with the following content:

To use markdown-it to compile the Markdown file, change the contents as follows:

Tip: While the sample is there to help with common configuration settings, IntelliSense is available for the tasks.json file as well to help you along. Use ⌃Space (Windows, Linux Ctrl+Space) to see the available settings.

Step 4: Run the Build Task

Since in more complex environments there can be more than one build task we prompt you to pick the task to execute after pressing ⇧⌘B (Windows, Linux Ctrl+Shift+B) (Run Build Task). In addition, we allow you to scan the output for compile problems. Since we only want to convert the Markdown file to HTML select Never scan the build output from the presented list.

At this point, you should see an additional file show up in the file list sample.html.

If you want to make the Compile Markdown task the default build task to run execute Configure Default Build Task from the global Terminal menu and select Compile Markdown from the presented list. The final tasks.json file will then look like this:

Automating Markdown compilation

Let's take things a little further and automate Markdown compilation with VS Code. We can do so with the same task runner integration as before, but with a few modifications.

Step 1: Install Gulp and some plug-ins

We use Gulp to create a task that automates Markdown compilation. We also use the gulp-markdown plug-in to make things a little easier.

We need to install gulp both globally (-g switch) and locally:

Note: gulp-markdown-it is a Gulp plug-in for the markdown-it module we were using before. There are many other Gulp Markdown plug-ins you can use, as well as plug-ins for Grunt.

You can test that your gulp installation was successful by typing gulp -v. You should see a version displayed for both the global (CLI) and local installations.

Step 2: Create a simple Gulp task

Open VS Code on the same folder from before (contains sample.md and tasks.json under the .vscode folder), and create gulpfile.js at the root.

Place the following source code in that file:

What is happening here?

  1. We are watching for changes to any Markdown file in our workspace, i.e. the current folder open in VS Code.
  2. We take the set of Markdown files that have changed, and run them through our Markdown compiler, i.e. gulp-markdown-it.
  3. We now have a set of HTML files, each named respectively after their original Markdown file. We then put these files in the same directory.

Step 3: Run the gulp default Task

To complete the tasks integration with VS Code, we will need to modify the task configuration from before to run the default Gulp task we just created. You can either delete the tasks.json file or empty it only keeping the 'version': '2.0.0' property. Now execute Run Task from the global Terminal menu. Observe that you are presented with a picker listing the tasks defined in the gulp file. Select gulp: default to start the task. We allow you to scan the output for compile problems. Since we only want to convert the Markdown file to HTML select Never scan the build output from the presented list. At this point, if you create and/or modify other Markdown files, you see the respective HTML files generated and/or changes reflected on save. You can also enable Auto Save to make things even more streamlined.

If you want to make the gulp: default task the default build task executed when pressing ⇧⌘B (Windows, Linux Ctrl+Shift+B) run Configure Default Build Task from the global Terminal menu and select gulp: default from the presented list. The final tasks.json file will then look like this:

Step 4: Terminate the gulp default Task

The gulp: default task runs in the background and watches for file changes to Markdown files. If you want to stop the task, you can use the Terminate Task from the global Terminal menu.

Next steps

Read on to find out about:

  • CSS, SCSS, and Less - Want to edit your CSS? VS Code has great support for CSS, SCSS, and Less editing.

Common questions

Is there spell checking?

Markdown Markup Languages

Not installed with VS Code but there are spell checking extensions. Check the VS Code Marketplace to look for useful extensions to help with your workflow.

Does VS Code support GitHub Flavored Markdown?

No, VS Code targets the CommonMark Markdown specification using the markdown-it library. GitHub is moving toward the CommonMark specification which you can read about in this update.

In the walkthrough above, I didn't find the Configure Task command in the Command Palette?

You may have opened a file in VS Code rather than a folder. You can open a folder by either selecting the folder with File > Open Folder or navigating to the folder and typing 'code .' at the command line.

Markup Syntax

Markup uses simple Markdown-like character delimiter syntax with custom extensions. There are four types of elements:

  • Single line elements format a line of text such as creating a heading.

  • Multiline elements format multiple lines of text such as creating lists or callouts.

  • Span elements format a span of characters such as adding emphasis.

  • Link elements add text based links such as a mailto, or inline assets such as images.

Single Line Elements

Single line elements use an opening delimiter, followed by at least one space and then the content.

Single line elements follow these rules:

  • The delimiter is the first non-whitespace character on the line.

  • There is at least one space between the delimiter and the content.

  • Extra whitespace between the delimiter and content is ignored.

  • The delimiter and all of the content are be terminated by a newlin.

For example, both of the following lines of markup render as the same second level heading as shown in Figure 4-1.

  1. //: ## Second Level Heading
  2. //: ## Second Level Heading

Note

Headings have an alternative syntax using a delimiter on the line below the content. For more information, see Headings.

Link Elements

Link elements define a link name and a link target. The link name is entered between an opening square bracket ([) and a closing square bracket (]). Asset links start with an exclamation mark (!) before the opening square bracket. The target specification for the link element is usually between an opening parentheses (() and a closing parentheses ()). Link Reference delimiters use a colon (:) followed by the target specification instead of parentheses.

Link elements follow these rules:

Markdown Vs Markup Language

  • The link element can appear anywhere on a line.

  • The delimiter and all of the content is on one line terminated by a newline.

Example: A URL Link

The following markup renders as a link to swift.org. The link shows as clickable blue text in the rendered documentation, as shown in Figure 4-2.

  1. //: Find more information for [Swift](http://swift.org)

Example: An Inline Image

This markup renders the image content of the link centered on the line after the word cat. The rest of the content is rendered on a line following the image as shown in Figure 4-3.

  1. //: Meet Swiftie the cat ![Swiftie the cat](cat.png) the real boss.

Multiline Elements

Multiline elements such as lists and callouts use an opening delimiter, followed by at least one space and then the content. The block of content continues until meeting a termination condition.

Multiline elements follow these rules:

  • The delimiter is the first non-whitespace character on the line.

  • There is at least one space between the delimiter and the content.

  • Extra whitespace between the delimiter and content is ignored.

  • A single newline followed by text wraps the text to the previous line.

  • A single empty line followed by text inserts the text as a new paragraph.

The delimiter formats all text until one of the following termination conditions:

  • Two or more consecutive empty lines

  • A different line or multiline delimiter at the same level of nesting.

Markdown Markup Language Examples

Nesting Delimiters

The nesting level of a multiline element increases for each set of four spaces or each tab preceding the opening delimiter.

Use nesting to:

  • Increase the indent level of a list

  • Add nested single line elements such as headings or horizontal rules

  • Add a Code Block

To add contiguous lines of nesting, add the same number of initial spaces or tabs on each line that starts with a delimiter. Empty lines do not need to be indented.

Example: A Simple Bulleted List

Markdown Formatting

The following markup renders in Quick Help as a bulleted list with two items as shown in Figure 4-4.

Example: Text Wrapping

The markup below renders as a single numbered list item as shown in Figure 4-5.

  1. /*:
  2. 1. This renders as
  3. one line in
  4. a numbered list
  5. */

Example: Inserting a Paragraph

This markup renders Line 4 as the second paragraph in the first bullet point as shown in Figure 4-6. The content for every paragraph is indented to the same level as the containing delimiter.

  1. /**
  2. * A bullet item
  3. With two paragraphs
  4. * The next bullet item
  5. */

Example: Terminating a Callout Using Two Empty Lines

The markup below renders as two different lists as shown in Figure 4-7. The first list is terminated by the two consecutive empty lines on lines 4 and 5 of the markup.

  1. /*:
  2. 1. First list, item 1
  3. 1. First list, item 2
  4. 1. Second list, item 1
  5. */

Example: Terminating a List with a Different Delimiter

This markup renders as two different lists, each with its own heading as shown in Figure 4-8. The heading delimiter on line 5 terminates the first list.

  1. /*:
  2. ### List A
  3. 1. Item 1, List A
  4. 1. Item 2, List A
  5. ### List B
  6. 1. Item 1, List B
  7. */

Example: Nesting Delimiters

This markup contains three kinds of nesting as seen in Figure 4-9.

  1. The nested list is a result of adding four spaces before the delimiters for each list item on lines 3 and 8.

  2. The nested code block is a result of indenting lines 5 and 6 by twelve spaces, or two levels of indentation from the enclosing delimiter.

  3. The nested horizontal rule is a result of indenting line 7 by eight spaces or one level of indentation from the enclosing delimiter. For more information on code blocks, see Code Block.

  1. /**
  2. 1. Level 1, Item 1
  3. 1. Level 2, Item 1
  4. func emptyFunc() {
  5. }
  6. - - -
  7. 1. Level 2, Item 2
  8. 1. Level 1, Item 2
  9. */

Span Elements

Span elements use the same delimiter at the start and end of the character span.

Span elements follow these rules:

  • The opening delimiter can appear anywhere on a line.

  • There is no space between the opening delimiter and the first character of the span.

  • There is no space between the first character and the closing delimiter of the span.

  • A single newline in the middle of a span is ignored and the text is wrapped to the first line.

  • Span elements can be combined.

Example: Ignoring Whitespace

The following markup results in one line of text as shown in Figure 4-10.

Example: Combining Elements

What Is A Markdown

This markup shows adding the delimiters for a span of bold characters inside the delimiters for a span with emphasis. The rendered result adds both emphasis and bold to inside other as seen in Figure 4-11.

  1. //: Span elements *nest **inside other** span* elements

Ignoring Single Newlines

Single newlines inside most line and text formatting delimiters are ignored as is whitespace at the end of a line.

For example, the following code ignores the newline at the end of Line 1, resulting in a single line of text with the words on one line rendered using the emphasis style. Adding whitespace at the end of the first line does not add spaces between the words on and one.

Exception: Links

Markup for textual links and media assets is on a single line of text that ends in a newline. The delimiter and arguments are treated as plain text if they span multiple lines.

Delimiters for textual links begin with an open square bracket ([). Delimiters for media assets begin with an exclamation mark followed by an open square bracket (![).

For example, in the markup below the delimiter and arguments for specifying an inline video player span lines 2 and 3. All three lines are treated as plain text and wrapped together into one rendered line of text as shown in Figure 4-12.

Markdown Markup Language Difference

  1. //: Watch the WWDC session introducing the San Francisco Font
  2. //: ![San Francisco font introduction]
  3. //: (new-fonts.mp4)

Combining all the lines into one line shows the video player.

Markup Vs Markdown

Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2017-06-05