|
| 1 | +/** |
| 2 | + * @luogu-discussion-archive/remark-lda-lfm |
| 3 | + * Copyright (c) 2025 Luogu Discussion Archive Project |
| 4 | + * See AUTHORS.txt in the project root for the full list of contributors. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * Please notice that 「洛谷」 (also known as "Luogu") is a registered trademark of |
| 20 | + * Shanghai Luogu Network Technology Co., Ltd (上海洛谷网络科技有限公司). |
| 21 | + * |
| 22 | + * @license AGPL-3.0-or-later |
| 23 | + */ |
| 24 | + |
| 25 | +/// <reference types="remark-parse" /> |
| 26 | +/// <reference types="remark-stringify" /> |
| 27 | +/// <reference types="mdast" /> |
| 28 | +/// <reference path="./index.d.ts" /> |
| 29 | + |
| 30 | +/** |
| 31 | + * @typedef {import('mdast').Root} Root |
| 32 | + * @typedef {import('vfile').VFile} VFile |
| 33 | + * @typedef {import('unified').Processor<Root>} Processor |
| 34 | + */ |
| 35 | + |
| 36 | +/** |
| 37 | + * @typedef Options |
| 38 | + * Configuration. |
| 39 | + * @property {RegExp[] | null} [linkRootToLuoguWhiteList] |
| 40 | + * URL patterns in list will not point to https://www.luogu.com.cn/, |
| 41 | + * except /user/${uid} (userMention). (optional). |
| 42 | + * @property {boolean | null} [userLinkPointToLuogu] |
| 43 | + * /user/${uid} (userMention) point to luogu or not. Default true. (optional) |
| 44 | + */ |
| 45 | + |
| 46 | +import { visit } from "unist-util-visit"; |
| 47 | + |
| 48 | +import { gfmFootnote } from "micromark-extension-gfm-footnote"; |
| 49 | +import { gfmStrikethrough } from "micromark-extension-gfm-strikethrough"; |
| 50 | +import { gfmTable } from "micromark-extension-gfm-table"; |
| 51 | +import { gfmAutolinkLiteral } from "micromark-extension-gfm-autolink-literal"; |
| 52 | + |
| 53 | +import { |
| 54 | + gfmAutolinkLiteralFromMarkdown, |
| 55 | + gfmAutolinkLiteralToMarkdown, |
| 56 | +} from "mdast-util-gfm-autolink-literal"; |
| 57 | +import { gfmTableFromMarkdown, gfmTableToMarkdown } from "mdast-util-gfm-table"; |
| 58 | +import { |
| 59 | + gfmStrikethroughFromMarkdown, |
| 60 | + gfmStrikethroughToMarkdown, |
| 61 | +} from "mdast-util-gfm-strikethrough"; |
| 62 | +import { |
| 63 | + gfmFootnoteFromMarkdown, |
| 64 | + gfmFootnoteToMarkdown, |
| 65 | +} from "mdast-util-gfm-footnote"; |
| 66 | + |
| 67 | +const mentionReg = /^\/user\/(\d+)$/; |
| 68 | +const legacyMentionReg = /^\/space\/show\?uid=(\d+)$/; |
| 69 | + |
| 70 | +/** @type {Options} */ |
| 71 | +const emptyOptions = {}; |
| 72 | + |
| 73 | +/** |
| 74 | + * remark-luogu-flavor plugin. |
| 75 | + * |
| 76 | + * @param {Options | null | undefined} [options] |
| 77 | + * Configuration (optional). |
| 78 | + * @this {Processor} |
| 79 | + */ |
| 80 | +export default function remarkLuoguFlavor(options) { |
| 81 | + const self = this; |
| 82 | + const settings = options || emptyOptions; |
| 83 | + const data = self.data(); |
| 84 | + |
| 85 | + const linkWhiteList = settings.linkRootToLuoguWhiteList ?? []; |
| 86 | + const userLinkPointToLuogu = settings.userLinkPointToLuogu ?? true; |
| 87 | + |
| 88 | + const micromarkExtensions = |
| 89 | + data.micromarkExtensions || (data.micromarkExtensions = []); |
| 90 | + const fromMarkdownExtensions = |
| 91 | + data.fromMarkdownExtensions || (data.fromMarkdownExtensions = []); |
| 92 | + const toMarkdownExtensions = |
| 93 | + data.toMarkdownExtensions || (data.toMarkdownExtensions = []); |
| 94 | + |
| 95 | + micromarkExtensions.push( |
| 96 | + gfmFootnote(), |
| 97 | + gfmStrikethrough({ singleTilde: false, ...settings }), |
| 98 | + gfmTable(), |
| 99 | + gfmAutolinkLiteral(), |
| 100 | + ); |
| 101 | + |
| 102 | + fromMarkdownExtensions.push( |
| 103 | + gfmFootnoteFromMarkdown(), |
| 104 | + gfmStrikethroughFromMarkdown(), |
| 105 | + gfmTableFromMarkdown(), |
| 106 | + gfmAutolinkLiteralFromMarkdown(), |
| 107 | + ); |
| 108 | + |
| 109 | + toMarkdownExtensions.push( |
| 110 | + gfmFootnoteToMarkdown(), |
| 111 | + gfmTableToMarkdown(), |
| 112 | + gfmStrikethroughToMarkdown(), |
| 113 | + gfmAutolinkLiteralToMarkdown(), |
| 114 | + ); |
| 115 | + |
| 116 | + /** |
| 117 | + * Transform. |
| 118 | + * |
| 119 | + * @param {Root} tree |
| 120 | + * Tree. |
| 121 | + * @returns {undefined} |
| 122 | + * Nothing. |
| 123 | + */ |
| 124 | + return (tree) => { |
| 125 | + visit(tree, "paragraph", (node) => { |
| 126 | + const childNode = node.children; |
| 127 | + childNode.forEach((child, index) => { |
| 128 | + const lastNode = childNode[index - 1]; |
| 129 | + if ( |
| 130 | + child.type === "link" && |
| 131 | + index >= 1 && |
| 132 | + lastNode.type === "text" && |
| 133 | + lastNode.value.endsWith("@") |
| 134 | + ) { |
| 135 | + const match = |
| 136 | + mentionReg.exec(child.url) ?? legacyMentionReg.exec(child.url); |
| 137 | + if (!match) return; |
| 138 | + /** @type {import("mdast").UserMention} */ |
| 139 | + const newNode = { |
| 140 | + type: "userMention", |
| 141 | + children: child.children, |
| 142 | + uid: parseInt(match[1]), |
| 143 | + data: { |
| 144 | + hName: "a", |
| 145 | + hProperties: { |
| 146 | + href: userLinkPointToLuogu |
| 147 | + ? `https://www.luogu.com.cn/user/${match[1]}` |
| 148 | + : `/user/${match[1]}`, |
| 149 | + "data-uid": match[1], |
| 150 | + class: "lfm-user-mention", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }; |
| 154 | + childNode[index] = newNode; |
| 155 | + } |
| 156 | + if (child.type === "image" && child.url.startsWith("bilibili:")) { |
| 157 | + let videoId = child.url.replace("bilibili:", ""); |
| 158 | + if (videoId.match(/^[0-9]/)) videoId = "av" + videoId; |
| 159 | + /** @type {import("mdast").BilibiliVideo} */ |
| 160 | + const newNode = { |
| 161 | + type: "bilibiliVideo", |
| 162 | + videoId, |
| 163 | + data: { |
| 164 | + hName: "iframe", |
| 165 | + hProperties: { |
| 166 | + scrolling: "no", |
| 167 | + allowfullscreen: "true", |
| 168 | + class: "lfm-bilibili-video", |
| 169 | + src: |
| 170 | + "https://www.bilibili.com/blackboard/webplayer/embed-old.html?bvid=" + |
| 171 | + videoId.replace(/[\?&]/g, "&"), |
| 172 | + }, |
| 173 | + }, |
| 174 | + }; |
| 175 | + childNode[index] = newNode; |
| 176 | + } |
| 177 | + }); |
| 178 | + }); |
| 179 | + visit(tree, "link", (node) => { |
| 180 | + if (!linkWhiteList.some((reg) => reg.test(node.url))) { |
| 181 | + try { |
| 182 | + node.url = new URL(node.url, "https://www.luogu.com.cn/").href; |
| 183 | + } catch (_) { |
| 184 | + // ignore |
| 185 | + } |
| 186 | + } |
| 187 | + }); |
| 188 | + }; |
| 189 | +} |
0 commit comments