Skip to content

Commit

Permalink
fix: fixed target and rel issue (fixes #1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikethsaha committed May 20, 2020
1 parent 9ff4d06 commit 3d662a5
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 95 deletions.
4 changes: 2 additions & 2 deletions packages/docsify-server-renderer/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { readFileSync } from 'fs';
import { resolve, basename } from 'path';
import resolvePathname from 'resolve-pathname';
import fetch from 'node-fetch';
import debug from 'debug';
import { AbstractHistory } from '../../src/core/router/history/abstract';
import { Compiler } from '../../src/core/render/compiler';
import { isAbsolutePath } from '../../src/core/router/util';
import * as tpl from '../../src/core/render/tpl';
import { prerenderEmbed } from '../../src/core/render/embed';
import fetch from 'node-fetch';
import debug from 'debug';

function cwd(...args) {
return resolve(process.cwd(), ...args);
Expand Down
2 changes: 1 addition & 1 deletion src/core/event/scroll.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Tweezer from 'tweezer.js';
import { isMobile } from '../util/env';
import * as dom from '../util/dom';
import { removeParams } from '../router/util';
import config from '../config';
import Tweezer from 'tweezer.js';

const nav = {};
let hoverOver = false;
Expand Down
4 changes: 2 additions & 2 deletions src/core/global-api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import prism from 'prismjs';
import marked from 'marked';
import * as util from './util';
import * as dom from './util/dom';
import { Compiler } from './render/compiler';
import { slugify } from './render/slugify';
import { get } from './fetch/ajax';
import prism from 'prismjs';
import marked from 'marked';

export default function() {
window.Docsify = {
Expand Down
5 changes: 3 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import marked from 'marked';
import { isAbsolutePath, getPath, getParentPath } from '../router/util';
import { isFn, merge, cached, isPrimitive } from '../util/core';
import { tree as treeTpl } from './tpl';
Expand All @@ -11,7 +12,6 @@ import { paragraphCompiler } from './compiler/paragraph';
import { taskListCompiler } from './compiler/taskList';
import { taskListItemCompiler } from './compiler/taskListItem';
import { linkCompiler } from './compiler/link';
import marked from 'marked';

const cachedLinks = {};

Expand Down Expand Up @@ -193,7 +193,7 @@ export class Compiler {

_initRenderer() {
const renderer = new marked.Renderer();
const { linkTarget, router, contentBase } = this;
const { linkTarget, linkRel, router, contentBase } = this;
const _self = this;
const origin = {};

Expand Down Expand Up @@ -233,6 +233,7 @@ export class Compiler {
renderer,
router,
linkTarget,
linkRel,
compilerClass: _self,
});
origin.paragraph = paragraphCompiler({ renderer });
Expand Down
25 changes: 19 additions & 6 deletions src/core/render/compiler/link.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { getAndRemoveConfig } from '../utils';
import { isAbsolutePath } from '../../router/util';

export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
export const linkCompiler = ({
renderer,
router,
linkTarget,
linkRel,
compilerClass,
}) =>
(renderer.link = (href, title = '', text) => {
let attrs = [];
const { str, config } = getAndRemoveConfig(title);

linkTarget = config.target || linkTarget;
linkRel =
linkTarget === '_blank'
? compilerClass.config.externalLinkRel || 'noopener'
: '';
title = str;

if (
Expand All @@ -24,10 +34,13 @@ export const linkCompiler = ({ renderer, router, linkTarget, compilerClass }) =>
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
}
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
}

if (config.target) {
attrs.push(`target="${config.target}"`);
attrs.push(
href.indexOf('mailto:') === 0
? ''
: linkRel !== ''
? ` rel="${linkRel}"`
: ''
);
}

// special case to check crossorigin urls
Expand Down
2 changes: 1 addition & 1 deletion src/core/render/embed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import stripIndent from 'strip-indent';
import { get } from '../fetch/ajax';
import { merge } from '../util/core';
import stripIndent from 'strip-indent';

const cached = {};

Expand Down
2 changes: 1 addition & 1 deletion src/core/render/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-unused-vars */
import tinydate from 'tinydate';
import * as dom from '../util/dom';
import cssVars from '../util/polyfill/css-vars';
import { callHook } from '../init/lifecycle';
Expand All @@ -10,7 +11,6 @@ import { scrollActiveSidebar } from '../event/scroll';
import { Compiler } from './compiler';
import * as tpl from './tpl';
import { prerenderEmbed } from './embed';
import tinydate from 'tinydate';

function executeScript() {
const script = dom
Expand Down
2 changes: 1 addition & 1 deletion test/unit/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require = require('esm')(
module /* , options */
); /* eslint-disable-line no-global-assign */
const { History } = require('../../src/core/router/history/base');
const { expect } = require('chai');
const { History } = require('../../src/core/router/history/base');

class MockHistory extends History {
parse(path) {
Expand Down
Loading

0 comments on commit 3d662a5

Please sign in to comment.