Skip to content

Commit

Permalink
feat(links): image-parameter#link
Browse files Browse the repository at this point in the history
  • Loading branch information
bhsd-harry committed Dec 20, 2024
1 parent c673a88 commit 611c400
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## v1.0.7

*2024-12-20*

**Added**

- Document link provider for the `link` parameter of images

## v1.0.5

*2024-12-06*
Expand Down
1 change: 1 addition & 0 deletions bump.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ else
if [[ $? -eq 0 ]]
then
sed -i '' -E "s/\"version\": \".+\"/\"version\": \"$1\"/" package.json
npm i --package-lock-only
git add -A
git commit -m "chore: bump version to $1"
git push
Expand Down
21 changes: 15 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@
"scripts": {
"vscode:prepublish": "npm run build",
"build": "tsc --project server/tsconfig.json",
"test": "mocha -A --no-diff --file server/dist/test/*.js --ignore=util.js",
"test": "mocha",
"lint:ts": "tsc --noEmit --project server/tsconfig.json && eslint --cache .",
"lint:json": "v8r -s server/src/data/schema/info.json server/src/data/*.json",
"lint": "npm run lint:ts && npm run lint:json"
},
"mocha": {
"asyncOnly": true,
"diff": false,
"spec": "server/dist/test/*.js",
"ignore": "server/dist/test/util.js"
},
"dependencies": {
"@bhsd/common": "^0.4.6",
"@bhsd/common": "^0.5.0",
"color-rgba": "^3.0.0",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-textdocument": "^1.0.12",
Expand Down
43 changes: 34 additions & 9 deletions server/src/links.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Parser from 'wikilint';
import {plainTypes, createNodeRange} from './util';
import {plainTypes, createNodeRange, createRange} from './util';
import {parse} from './tasks';
import type {Token, AttributeToken, TokenTypes} from 'wikilint';
import type {DocumentLink, TextDocumentIdentifier} from 'vscode-languageserver/node';

const srcTags = new Set(['templatestyles', 'img']),
citeTags = new Set(['blockquote', 'del', 'ins', 'q']),
linkTypes = new Set<TokenTypes>(['link-target', 'template-name', 'invoke-module']);
linkTypes = new Set<TokenTypes>(['link-target', 'template-name', 'invoke-module']),
protocolRegex = new RegExp(`^(?:${Parser.getConfig().protocol}|//)`, 'iu');

const getUrl = (path: string, page: string, ns?: number): string => {
const {title, fragment} = Parser.normalizeTitle(page, ns),
encoded = encodeURIComponent(title) + (fragment === undefined ? '' : `#${encodeURIComponent(fragment)}`);
const {title, fragment, valid} = Parser.normalizeTitle(page, ns);
if (!valid) {
throw new RangeError('Invalid page name.');
}
const encoded = encodeURIComponent(title) + (fragment === undefined ? '' : `#${encodeURIComponent(fragment)}`);
return path.includes('$1') ? path.replace('$1', encoded) : path + (path.endsWith('/') ? '' : '/') + encoded;
};

Expand All @@ -26,20 +30,30 @@ const parseMagicLink = (path: string, link: string): string => {
export const provideLinks = async ({uri}: TextDocumentIdentifier, path: string): Promise<DocumentLink[]> => {
const root = await parse(uri);
return root
.querySelectorAll('link-target,template-name,invoke-module,magic-link,ext-link-url,free-ext-link,attr-value')
.querySelectorAll(
'link-target,template-name,invoke-module,magic-link,ext-link-url,free-ext-link,attr-value,'
+ 'image-parameter#link',
)
.filter(({type, parentNode, childNodes}) => {
const {name, tag} = parentNode as AttributeToken;
return (type !== 'attr-value' || name === 'src' && srcTags.has(tag) || name === 'cite' && citeTags.has(tag))
&& childNodes.every(({type: t}) => plainTypes.has(t));
})
.flatMap((token: Token & {toString(skip?: boolean): string}) => {
const {type, parentNode} = token,
const {type, parentNode, firstChild, lastChild} = token,
{name, tag} = parentNode as AttributeToken;
let target = token.toString(true).trim();
let target = type === 'image-parameter'
? (Object.getPrototypeOf(token.constructor) as ObjectConstructor).prototype
.toString.apply(token, [true] as unknown as []).trim()
: token.toString(true).trim();
try {
if (type === 'magic-link') {
target = parseMagicLink(path, target);
} else if (linkTypes.has(type) || type === 'attr-value' && name === 'src' && tag === 'templatestyles') {
} else if (
linkTypes.has(type)
|| type === 'attr-value' && name === 'src' && tag === 'templatestyles'
|| type === 'image-parameter' && !protocolRegex.test(target)
) {
if (target.startsWith('/')) {
return [];
}
Expand All @@ -54,7 +68,18 @@ export const provideLinks = async ({uri}: TextDocumentIdentifier, path: string):
if (target.startsWith('//')) {
target = `https:${target}`;
}
return [{range: createNodeRange(root, token), target: new URL(target).href}];
return [
{
range: type === 'image-parameter'
? createRange(
root,
firstChild!.getAbsoluteIndex(),
lastChild!.getAbsoluteIndex() + String(lastChild!).length,
)
: createNodeRange(root, token),
target: new URL(target).href,
},
];
} catch {
return [];
}
Expand Down
30 changes: 30 additions & 0 deletions server/src/test/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ISBN 1-2-3-4-5-6-7-8-9-0
News:e
<templatestyles src = f />
<q cite = "HTTPS://G/G">
[[file:a|link=a]]
[[file:a|link=//a]]
`,
results: DocumentLink[] = [
{
Expand Down Expand Up @@ -86,6 +88,34 @@ News:e
},
target: 'https://g/G',
},
{
range: {
start: {line: 11, character: 2},
end: {line: 11, character: 8},
},
target: 'https://mediawiki.org/wiki/File%3AA',
},
{
range: {
start: {line: 11, character: 14},
end: {line: 11, character: 15},
},
target: 'https://mediawiki.org/wiki/A',
},
{
range: {
start: {line: 12, character: 2},
end: {line: 12, character: 8},
},
target: 'https://mediawiki.org/wiki/File%3AA',
},
{
range: {
start: {line: 12, character: 14},
end: {line: 12, character: 17},
},
target: 'https://a/',
},
];

describe('documentLinkProvider', () => {
Expand Down

0 comments on commit 611c400

Please sign in to comment.