add link_renderer to handle external links

pull/2411/head
AlexxNB 6 years ago
parent 3f97558533
commit ab7a9e7da7

@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { extract_frontmatter, langs } from '../../utils/markdown.js';
import { extract_frontmatter, langs, link_renderer } from '../../utils/markdown.js';
import marked from 'marked';
import PrismJS from 'prismjs';
import 'prismjs/components/prism-bash';
@ -20,6 +20,8 @@ export default function() {
const renderer = new marked.Renderer();
renderer.link = link_renderer;
renderer.code = (source, lang) => {
const plang = langs[lang];
const highlighted = PrismJS.highlight(

@ -1,6 +1,6 @@
import fs from 'fs';
import path from 'path';
import { extract_frontmatter, extract_metadata, langs } from '../../utils/markdown.js';
import { extract_frontmatter, extract_metadata, langs, link_renderer } from '../../utils/markdown.js';
import marked from 'marked';
import PrismJS from 'prismjs';
import 'prismjs/components/prism-bash';
@ -50,6 +50,8 @@ export default function() {
let block_open = false;
renderer.link = link_renderer;
renderer.hr = (...args) => {
block_open = true;

@ -2,7 +2,7 @@ import * as fs from 'fs';
import * as path from 'path';
import marked from 'marked';
import PrismJS from 'prismjs';
import { extract_frontmatter, extract_metadata, langs } from '../../../utils/markdown';
import { extract_frontmatter, extract_metadata, langs, link_renderer } from '../../../utils/markdown';
const cache = new Map();
@ -33,6 +33,8 @@ function get_tutorial(slug) {
const renderer = new marked.Renderer();
renderer.link = link_renderer;
renderer.code = (source, lang) => {
source = source.replace(/^ +/gm, match =>
match.split(' ').join('\t')

@ -42,3 +42,20 @@ export const langs = {
js: 'javascript',
css: 'css'
};
// links renderer
export function link_renderer(href,title,text) {
let target_attr = '';
let title_attr = '';
if(href.startsWith("http")) {
target_attr = ' target="_blank"';
}
if(title !== null) {
title_attr = ` title="${title}"`;
}
return `<a href="${href}"${target_attr}${title_attr}>${text}</a>`;
};
Loading…
Cancel
Save