Skip to content

Commit 596cbf5

Browse files
committed
Fix for linking absolute paths in file:// scheme
1 parent ac3c6d6 commit 596cbf5

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/parser/parse-matches.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ export function parseMatches(text: string, args: ParseMatchesArgs): Match[] {
239239

240240
// For debugging: search for and uncomment other "For debugging" lines
241241
// table.push([
242-
// charIdx,
242+
// String(charIdx),
243243
// char,
244244
// `10: ${char.charCodeAt(0)}\n0x: ${char.charCodeAt(0).toString(16)}\nU+${char.codePointAt(0)}`,
245245
// stateMachines.map(machine => `${machine.type}${'matchType' in machine ? ` (${machine.matchType})` : ''}`).join('\n') || '(none)',
246246
// stateMachines.map(machine => State[machine.state]).join('\n') || '(none)',
247-
// charIdx,
247+
// String(charIdx),
248248
// stateMachines.map(m => m.startIdx).join('\n'),
249249
// stateMachines.map(m => m.acceptStateReached).join('\n'),
250250
// ]);
@@ -382,10 +382,11 @@ export function parseMatches(text: string, args: ParseMatchesArgs): Match[] {
382382

383383
function stateSchemeSlash2(stateMachine: StateMachine, char: string) {
384384
if (char === '/') {
385-
// 3rd slash, must be an absolute path (path-absolute in the
386-
// ABNF), such as in a file:///c:/windows/etc. See
385+
// 3rd slash, must be an absolute path (`path-absolute` in the
386+
// ABNF), such as in "file:///c:/windows/etc". See
387387
// https://tools.ietf.org/html/rfc3986#appendix-A
388388
stateMachine.state = State.Path;
389+
stateMachine.acceptStateReached = true;
389390
} else if (isDomainLabelStartChar(char)) {
390391
// start of "authority" section - see https://tools.ietf.org/html/rfc3986#appendix-A
391392
stateMachine.state = State.DomainLabelChar;
@@ -1092,7 +1093,7 @@ export function excludeUnbalancedTrailingBracesAndPunctuation(matchedText: strin
10921093
}
10931094

10941095
// States for the parser
1095-
// For debugging: temporarily remove 'const'
1096+
// For debugging: temporarily remove `const` from `const enum`
10961097
const enum State {
10971098
// Scheme states
10981099
SchemeChar = 0, // First char must be an ASCII letter. Subsequent characters can be: ALPHA / DIGIT / "+" / "-" / "."

tests/autolinker-url.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,19 @@ describe('Autolinker Url Matching >', () => {
269269
const result = autolinker.link('file:some-file.txt mid-sentence');
270270

271271
// TODO: in theory this is a valid url, but do we want to continue
272-
// autolinking this in the future?
272+
// autolinking this in the future? Seems like Autolinker may link
273+
// "too much" in this case, such as "things:stuff"
273274
expect(result).toBe(`<a href="file:some-file.txt">file:some-file.txt</a> mid-sentence`);
274275
});
275276

277+
it('should match a file url with an absolute path', function () {
278+
const result = autolinker.link('check out file:///c:/windows/etc mid-sentence');
279+
280+
expect(result).toBe(
281+
`check out <a href="file:///c:/windows/etc">file:///c:/windows/etc</a> mid-sentence`
282+
);
283+
});
284+
276285
it("should NOT autolink possible URLs with the 'javascript:' URI scheme", () => {
277286
const result = autolinker.link("do not link javascript:window.alert('hi') please");
278287
expect(result).toBe("do not link javascript:window.alert('hi') please");

0 commit comments

Comments
 (0)