UNPKG

5.73 kBTypeScriptView Raw
1import { AnchorTagBuilder } from "../anchor-tag-builder";
2/**
3 * @abstract
4 * @class Autolinker.match.Match
5 *
6 * Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a
7 * {@link Autolinker#replaceFn replaceFn}, and may be used to query for details about the match.
8 *
9 * For example:
10 *
11 * var input = "..."; // string with URLs, Email Addresses, and Mentions (Twitter, Instagram, Soundcloud)
12 *
13 * var linkedText = Autolinker.link( input, {
14 * replaceFn : function( match ) {
15 * console.log( "href = ", match.getAnchorHref() );
16 * console.log( "text = ", match.getAnchorText() );
17 *
18 * switch( match.getType() ) {
19 * case 'url' :
20 * console.log( "url: ", match.getUrl() );
21 *
22 * case 'email' :
23 * console.log( "email: ", match.getEmail() );
24 *
25 * case 'mention' :
26 * console.log( "mention: ", match.getMention() );
27 * }
28 * }
29 * } );
30 *
31 * See the {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.
32 */
33export declare abstract class Match {
34 /**
35 * @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
36 *
37 * Reference to the AnchorTagBuilder instance to use to generate an anchor
38 * tag for the Match.
39 */
40 private __jsduckDummyDocProp;
41 private readonly tagBuilder;
42 /**
43 * @cfg {String} matchedText (required)
44 *
45 * The original text that was matched by the {@link Autolinker.matcher.Matcher}.
46 */
47 protected readonly matchedText: string;
48 /**
49 * @cfg {Number} offset (required)
50 *
51 * The offset of where the match was made in the input string.
52 */
53 private offset;
54 /**
55 * @member Autolinker.match.Match
56 * @method constructor
57 * @param {Object} cfg The configuration properties for the Match
58 * instance, specified in an Object (map).
59 */
60 constructor(cfg: MatchConfig);
61 /**
62 * Returns a string name for the type of match that this class represents.
63 *
64 * @abstract
65 * @return {String}
66 */
67 abstract getType(): string;
68 /**
69 * Returns the original text that was matched.
70 *
71 * @return {String}
72 */
73 getMatchedText(): string;
74 /**
75 * Sets the {@link #offset} of where the match was made in the input string.
76 *
77 * A {@link Autolinker.matcher.Matcher} will be fed only HTML text nodes,
78 * and will therefore set an original offset that is relative to the HTML
79 * text node itself. However, we want this offset to be relative to the full
80 * HTML input string, and thus if using {@link Autolinker#parse} (rather
81 * than calling a {@link Autolinker.matcher.Matcher} directly), then this
82 * offset is corrected after the Matcher itself has done its job.
83 *
84 * @param {Number} offset
85 */
86 setOffset(offset: number): void;
87 /**
88 * Returns the offset of where the match was made in the input string. This
89 * is the 0-based index of the match.
90 *
91 * @return {Number}
92 */
93 getOffset(): number;
94 /**
95 * Returns the anchor href that should be generated for the match.
96 *
97 * @abstract
98 * @return {String}
99 */
100 abstract getAnchorHref(): string;
101 /**
102 * Returns the anchor text that should be generated for the match.
103 *
104 * @abstract
105 * @return {String}
106 */
107 abstract getAnchorText(): string;
108 /**
109 * Returns the CSS class suffix(es) for this match.
110 *
111 * A CSS class suffix is appended to the {@link Autolinker#className} in
112 * the {@link Autolinker.AnchorTagBuilder} when a match is translated into
113 * an anchor tag.
114 *
115 * For example, if {@link Autolinker#className} was configured as 'myLink',
116 * and this method returns `[ 'url' ]`, the final class name of the element
117 * will become: 'myLink myLink-url'.
118 *
119 * The match may provide multiple CSS class suffixes to be appended to the
120 * {@link Autolinker#className} in order to facilitate better styling
121 * options for different match criteria. See {@link Autolinker.match.Mention}
122 * for an example.
123 *
124 * By default, this method returns a single array with the match's
125 * {@link #getType type} name, but may be overridden by subclasses.
126 *
127 * @return {String[]}
128 */
129 getCssClassSuffixes(): string[];
130 /**
131 * Builds and returns an {@link Autolinker.HtmlTag} instance based on the
132 * Match.
133 *
134 * This can be used to easily generate anchor tags from matches, and either
135 * return their HTML string, or modify them before doing so.
136 *
137 * Example Usage:
138 *
139 * var tag = match.buildTag();
140 * tag.addClass( 'cordova-link' );
141 * tag.setAttr( 'target', '_system' );
142 *
143 * tag.toAnchorString(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
144 *
145 * Example Usage in {@link Autolinker#replaceFn}:
146 *
147 * var html = Autolinker.link( "Test google.com", {
148 * replaceFn : function( match ) {
149 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
150 * tag.setAttr( 'rel', 'nofollow' );
151 *
152 * return tag;
153 * }
154 * } );
155 *
156 * // generated html:
157 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
158 */
159 buildTag(): import("..").HtmlTag;
160}
161export interface MatchConfig {
162 tagBuilder: AnchorTagBuilder;
163 matchedText: string;
164 offset: number;
165}