Skip to content

Commit

Permalink
docs(chore): remove markdown-it
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ksem committed May 11, 2023
1 parent 053b086 commit 78e2034
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 123 deletions.
6 changes: 3 additions & 3 deletions packages/docs/modules/markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export default defineNuxtModule({

setup() {
addImports({
name: 'useMarkdownIt',
as: 'useMarkdownIt',
from: resolve(__dirname, './runtime/useMarkdownIt'),
name: 'useMarkdown',
as: 'useMarkdown',
from: resolve(__dirname, './runtime/useMarkdown'),
})
}
})
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { it, describe, expect } from 'vitest';
import { marked } from 'marked';
import { externalLinkMarkedPlugin } from './external-links';
import { fixTargetLinks } from './external-links';

describe('externalLinkMarkedPlugin', () => {
it('targetBlankPlugin: adds target="_blank" to links', () => {
marked.use(externalLinkMarkedPlugin());

const markdown = `[vue-press](https://vuepress.vuejs.org/)[[target=_blank]]`;
const html = marked(markdown);
const html = fixTargetLinks(marked(markdown));

expect(/target="_blank"/.test(html)).toBeTruthy();
});
Expand Down
47 changes: 1 addition & 46 deletions packages/docs/modules/markdown/runtime/plugins/external-links.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Renderer, Parser } from 'marked'

const addAttrToLink = (str: string, attr: string) => {
const [start, end] = str.split('<a')
return start + '<a ' + attr + end
Expand All @@ -16,54 +14,11 @@ export const fixTargetLinks = (textToRender: string) => {
if (target === 'target=_blank') {
target = 'target="_blank"';
}
const [link, attr] = matchedTarget
const [link] = matchedTarget

textToRender = addAttrToLink(textToRender
.replace(link, link.replace(targetRegex, '')), target)
}

return textToRender;
}

export function externalLinkMarkedPlugin() {
const renderer = new Renderer();
const paragraphRenderer = renderer.html;

renderer.html = function (text) {
let textToRender = paragraphRenderer.call(renderer, text);

const targetRegex = /\[\[(.*?)\]\]/g; // Search for the [[target="_blank"]] in the markdown.
const linkRegexPattern = /<a\s+(?:[^>]*?\s+)?href=(["'])(.*?)\1/g; // Searching for all <a href=""> tags

const targets: string[] = [];

const links = [];

let matchedTarget;

let matchedLink;

while ((matchedTarget = targetRegex.exec(textToRender)) !== null) {
let target = matchedTarget[1].trim().replaceAll('&quot;', '"')
if (target === 'target=_blank') {
target = 'target="_blank"';
}
targets.push(target);
}

if (targets.length > 0) {
while ((matchedLink = linkRegexPattern.exec(textToRender)) !== null) {
links.push(matchedLink[0]);
}

links.forEach((fndLink, index) => {
const link = fndLink.replace(/^<a /, `<a ${targets[index]} rel="nofollow" `);
textToRender = textToRender.replaceAll(targetRegex, '').replace(fndLink, link);
});
}

return textToRender;
};

return { renderer };
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { InjectionKey } from "vue"
import { marked } from 'marked';
import { externalLinkMarkedPlugin, fixTargetLinks } from "./plugins/external-links";
import { fixTargetLinks } from "./plugins/external-links";
import { localizedLinkMarkedPlugin } from './plugins/localized-links'

let localizePlugin: null | ReturnType<typeof localizedLinkMarkedPlugin> = null

export const useMarkdownProvideKey = 'vuestic:markdown' as unknown as InjectionKey<ReturnType<typeof marked>>

export const useMarkdownIt = () => {
export const useMarkdown = () => {
const { locale } = useI18n()

if (!localizePlugin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
defineProps({
text: {
type: String,
required: true,
}
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<script lang="ts" setup>
import { computed } from "vue";
import { useColors, useElementTextColor } from 'vuestic-ui'
import { useColors } from "vuestic-ui";
const props = defineProps({
content: {
Expand All @@ -26,40 +26,43 @@ const props = defineProps({
background: {
type: String,
default: undefined,
}
},
});
const { locale } = useI18n();
const { parse } = useMarkdownIt();
const { parse, parseInline } = useMarkdown();
const text = computed(() => {
// return props.content
try {
if (!props.content) { return '' }
if (!props.content) {
return "";
}
if (props.inline) {
return parseInline(props.content);
}
if (props.text) {
return (parse(props.content)).match(/<p>(.*)<\/p>/)?.[1] || ''
return parse(props.content).match(/<p>(.*)<\/p>/)?.[1] || "";
}
return (parse(props.content));
return parse(props.content);
} catch (e) {
console.error(e, props.content)
return ''
console.error(e, props.content);
return "";
}
});
const { getColor, setHSLAColor, getTextColor, colors } = useColors()
const { getColor, setHSLAColor, getTextColor, colors } = useColors();
const codeBackground = computed(() => {
return setHSLAColor(colors.backgroundElement, { })
})
return setHSLAColor(colors.backgroundElement, {});
});
const codeBorder = computed(() => {
const textColor = getColor(getTextColor(codeBackground.value))
const textColor = getColor(getTextColor(codeBackground.value));
return setHSLAColor(textColor, { a: 0.1 })
})
return setHSLAColor(textColor, { a: 0.1 });
});
</script>

<style lang="scss">
Expand All @@ -78,7 +81,8 @@ const codeBorder = computed(() => {
code {
display: inline-block;
font-family: Source Code Pro, Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
font-family: Source Code Pro, Consolas, Monaco, "Andale Mono", "Ubuntu Mono",
monospace;
background: var(--code-bg, v-bind(codeBackground));
font-size: 0.85rem;
line-height: 100%;
Expand All @@ -100,17 +104,17 @@ const codeBorder = computed(() => {
span {
color: currentColor;
}
}
a[target="_blank"] {
position: relative;
&::after {
content: "\279A";
a[target="_blank"] {
position: relative;
opacity: 0.35;
line-height: 1;
vertical-align: text-top;
&::after {
content: "\279A";
position: relative;
opacity: 0.35;
line-height: 1;
vertical-align: text-top;
}
}
}
</style>
4 changes: 1 addition & 3 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@nuxtjs/i18n": "^8.0.0-beta.11",
"@nuxtjs/tailwindcss": "^6.3.0",
"@types/escodegen": "^0.0.7",
"@types/marked": "^4.3.0",
"@types/estree": "^1.0.0",
"acorn": "^8.8.1",
"acorn-walk": "^8.2.0",
Expand All @@ -42,12 +43,9 @@
"@docsearch/js": "^3.2.1",
"@nuxtjs/google-fonts": "^3.0.0-1",
"@types/acorn": "^6.0.0",
"@types/marked": "^4.3.0",
"acorn": "^8.8.1",
"codesandbox": "^2.2.3",
"highlight.js": "^11.7.0",
"markdown-it": "^12.3.2",
"markdown-it-attrs": "^4.0.0",
"marked": "^4.3.0"
}
}
38 changes: 0 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7465,11 +7465,6 @@ entities@^4.2.0:
resolved "https://registry.yarnpkg.com/entities/-/entities-4.5.0.tgz#5d268ea5e7113ec74c4d033b79ea5a35a488fb48"
integrity sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==

entities@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/entities/-/entities-2.1.0.tgz#992d3129cf7df6870b96c57858c249a120f8b8b5"
integrity sha512-hCx1oky9PFrJ611mf0ifBLBRW8lUUVRlFolb5gWRfIELabBlbp9xZvrqZLZAs+NxFnbfQoeGd8wDkygjg7U85w==

env-paths@^2.2.0:
version "2.2.1"
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
Expand Down Expand Up @@ -11233,13 +11228,6 @@ lines-and-columns@^1.1.6:
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.2.4.tgz#eca284f75d2965079309dc0ad9255abb2ebc1632"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==

linkify-it@^3.0.1:
version "3.0.3"
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-3.0.3.tgz#a98baf44ce45a550efb4d49c769d07524cc2fa2e"
integrity sha512-ynTsyrFSdE5oZ/O9GEf00kPngmOfVwazR5GKDq6EYfhlpFug3J2zybX56a2PRRpc9P+FuSoGNAwjlbDs9jJBPQ==
dependencies:
uc.micro "^1.0.1"

lint-staged@^11.1.2:
version "11.2.6"
resolved "https://registry.yarnpkg.com/lint-staged/-/lint-staged-11.2.6.tgz#f477b1af0294db054e5937f171679df63baa4c43"
Expand Down Expand Up @@ -11745,22 +11733,6 @@ map-visit@^1.0.0:
dependencies:
object-visit "^1.0.0"

markdown-it-attrs@^4.0.0:
version "4.1.6"
resolved "https://registry.yarnpkg.com/markdown-it-attrs/-/markdown-it-attrs-4.1.6.tgz#2bc331c7649d8c6396a0613c2bba1093f3e64da9"
integrity sha512-O7PDKZlN8RFMyDX13JnctQompwrrILuz2y43pW2GagcwpIIElkAdfeek+erHfxUOlXWPsjFeWmZ8ch1xtRLWpA==

markdown-it@^12.3.2:
version "12.3.2"
resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-12.3.2.tgz#bf92ac92283fe983fe4de8ff8abfb5ad72cd0c90"
integrity sha512-TchMembfxfNVpHkbtriWltGWc+m3xszaRD0CZup7GFFhzIgQqxIfn3eGj1yZpfuflzPvfkt611B2Q/Bsk1YnGg==
dependencies:
argparse "^2.0.1"
entities "~2.1.0"
linkify-it "^3.0.1"
mdurl "^1.0.1"
uc.micro "^1.0.5"

marked@^4.3.0:
version "4.3.0"
resolved "https://registry.yarnpkg.com/marked/-/marked-4.3.0.tgz#796362821b019f734054582038b116481b456cf3"
Expand Down Expand Up @@ -11826,11 +11798,6 @@ [email protected]:
resolved "https://registry.yarnpkg.com/mdn-data/-/mdn-data-2.0.4.tgz#699b3c38ac6f1d728091a64650b65d388502fd5b"
integrity sha512-iV3XNKw06j5Q7mi6h+9vbx23Tv7JkjEVgKHW4pimwyDGWm0OIQntJJ+u1C6mg6mK1EaTv42XQ7w76yuzH7M2cA==

mdurl@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/mdurl/-/mdurl-1.0.1.tgz#fe85b2ec75a59037f2adfec100fd6c601761152e"
integrity sha512-/sKlQJCBYVY9Ers9hqzKou4H6V5UWc/M59TH2dvkt+84itfnq7uFOMLpOiOS4ujvHP4etln18fmIxA5R5fll0g==

[email protected]:
version "0.3.0"
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
Expand Down Expand Up @@ -17433,11 +17400,6 @@ typescript@^5, typescript@^5.0.4:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b"
integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw==

uc.micro@^1.0.1, uc.micro@^1.0.5:
version "1.0.6"
resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac"
integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA==

ufo@^0.8.3:
version "0.8.6"
resolved "https://registry.yarnpkg.com/ufo/-/ufo-0.8.6.tgz#c0ec89bc0e0c9fa59a683680feb0f28b55ec323b"
Expand Down

0 comments on commit 78e2034

Please sign in to comment.