@diplodoc/transform 4.64.1 → 4.65.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -74,7 +74,7 @@ export function headingInfo(tokens: Token[], idx: number) {
74
74
  }
75
75
 
76
76
  export function isExternalHref(href: string) {
77
- return href.startsWith('http') || href.startsWith('//');
77
+ return /^(\w{1,10}:)?\/\//.test(href) || /^([+\w]{1,10}:)/.test(href);
78
78
  }
79
79
 
80
80
  export function transformLinkToOriginalArticle(opts: {root: string; currentPath: string}) {
@@ -113,3 +113,40 @@ export function defaultTransformLink(href: string) {
113
113
 
114
114
  return href;
115
115
  }
116
+
117
+ type TokenWalker = (
118
+ token: Token,
119
+ state: {commented: boolean; index: number},
120
+ ) => void | undefined | {skip: number};
121
+
122
+ export function filterTokens(tokens: Token[], type: string, handler: TokenWalker) {
123
+ let commented = false;
124
+
125
+ if (!tokens || !tokens.length) {
126
+ return;
127
+ }
128
+
129
+ for (let index = 0; index < tokens.length; index++) {
130
+ const token = tokens[index];
131
+
132
+ if (token.type === 'html_block') {
133
+ const commentStart = token.content.match('<!--');
134
+ const commentEnd = token.content.match('-->');
135
+
136
+ if (commentStart && !commentEnd) {
137
+ commented = true;
138
+ }
139
+
140
+ if (!commentStart && commentEnd) {
141
+ commented = false;
142
+ }
143
+ }
144
+
145
+ if (token.type === type) {
146
+ const result = handler(token, {commented, index});
147
+ if (result?.skip) {
148
+ index += result?.skip - 1;
149
+ }
150
+ }
151
+ }
152
+ }