Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
special-kate committed Dec 11, 2022
2 parents 654a12c + 1ac8ee0 commit 6f364a4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/content-script/index.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import './styles.css'
import 'github-markdown-css'
import './katex.less'
import MarkdownIt from 'markdown-it'
import MarkdownItTexmath from "markdown-it-texmath";
import Katex from "katex"
import Browser from 'webextension-polyfill'
import { getMarkdownRenderer } from './markdown.mjs'
import { config } from './search-engine-configs.mjs'
import { getPossibleElementByQuerySelector } from './utils.mjs'

async function run(question, siteConfig) {
const markdown = new MarkdownIt().use(MarkdownItTexmath, {
const markdown = getMarkdownRenderer().use(MarkdownItTexmath, {
engine: Katex,
delimiters: 'dollars',
katexOptions: { macros: { "\\RR": "\\mathbb{R}" }, throwOnError: false }
Expand Down
32 changes: 32 additions & 0 deletions src/content-script/markdown.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import MarkdownIt from 'markdown-it'

export function getMarkdownRenderer() {
const markdown = new MarkdownIt({
linkify: true,
})

// source: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer

// Remember old renderer, if overridden, or proxy to default renderer
const defaultRender =
markdown.renderer.rules.link_open ||
function (tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options)
}

markdown.renderer.rules.link_open = function (tokens, idx, options, env, self) {
// If you are sure other plugins can't add `target` - drop check below
const aIndex = tokens[idx].attrIndex('target')

if (aIndex < 0) {
tokens[idx].attrPush(['target', '_blank']) // add new attribute
} else {
tokens[idx].attrs[aIndex][1] = '_blank' // replace value of existing attr
}

// pass token to default renderer.
return defaultRender(tokens, idx, options, env, self)
}

return markdown
}

0 comments on commit 6f364a4

Please sign in to comment.