test
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

merging

12Me21 e2e71c60 b6c9a0c2

+72 -56
+14 -10
facet.js
··· 1 - function merge_features_to_facets(features) { 1 + export function merge_features_to_facets(features) { 2 2 // at begin, push its type to stack 3 3 // at end, pop 4 4 // make list of begins and ends ··· 6 6 // iterate 7 7 let edges = [] 8 8 for (let x of features) { 9 - edges.push([x.start,'start',x]) 10 - edges.push([x.end,'end',x]) 9 + edges.push([x.start,x]) 10 + edges.push([x.end,x]) 11 11 } 12 - edges.sort((a,b)=>{a[0]-b[0]}) 12 + edges.sort(([a],[b])=>a-b) 13 13 let open = new Set() 14 14 let facets = [] 15 15 let last = 0 16 - for (let [index,type,feature] of edges) { 17 - facets.push({ 18 - range: [last, index], 19 - features: [...open], 20 - }) 16 + for (let [index,feature] of edges) { 17 + console.log(index) 18 + if (index > last && open.size) { 19 + facets.push({ 20 + range: [last, index], 21 + features: [...open], 22 + }) 23 + } 21 24 open.delete(feature) || open.add(feature) 22 25 last = index 23 26 } 24 27 return facets 25 28 } 26 29 27 - spans = [ 30 + /*spans = [ 28 31 {type:"abc", start: 1, end: 5}, 29 32 {type:"def", start: 3, end: 5}, 30 33 {type:"zzz", start: 6, end: 10}, ··· 38 41 {start: 3, end: 5, types: ["abc","def"]}, 39 42 {start: 6, end: 10, types: ["zzz"]}, 40 43 ] 44 + */
+37 -46
parse.js
··· 1 - class MarkupLanguage { 2 - big_regex = /(?:)/g 3 - constructor() {} 4 - process_match(match) { 5 - return [{text:match[0], features:[]}] 1 + export class MarkupParser { 2 + constructor(big_regex, process_match) { 3 + this.process_match = process_match 4 + this.big_regex = big_regex 6 5 } 7 6 text_to_segments(text) { 8 7 let segments = [] ··· 21 20 } 22 21 } 23 22 24 - class Markup1 extends MarkupLanguage { 25 - big_regex = ()=>{ 26 - let r = String.raw 27 - let whitespace = r`\s\u00AD\u2060\u200A\u200B\u200C\u200D\u20e2` 28 - let url = r`[-\w/%&=#+~@$*'!?,.;:]*` 29 - let url_final = r`[-\w/%&=#+~@$*']` 30 - return new RegExp([ 31 - r`\b(?<link>https?://${url}${url_final}([(]${url}[)](${url}${url_final})?)?)(?:\[(?<link_text>.*?)\])?`, 32 - r`(?<=^|\s)[##](?<hashtag>(?!\uFE0F)[^${whitespace}]*[^\p{P}${whitespace}])`, // note: must filter out #123 33 - r`(?<=^|\s|[(])@(?<mention>[-a-zA-Z0-9]+([.][-a-zA-Z0-9]+)+)\b`, 34 - ].join("|"), 'gu') 35 - }() 36 - process_match(match) { 37 - let g = match.groups 38 - if (g.mention!=null) { 39 - return [{text:match[0], features:[{ 40 - $type: 'app.bsky.richtext.facet#mention', 41 - did: g.mention, // hack, DID must be resolved afterwards 23 + let r = String.raw 24 + let whitespace = r`\s\u00AD\u2060\u200A\u200B\u200C\u200D\u20e2` 25 + let url = r`[-\w/%&=#+~@$*'!?,.;:]*` 26 + let url_final = r`[-\w/%&=#+~@$*']` 27 + let big_regex = RegExp([ 28 + r`\b(?<link>https?://${url}${url_final}([(]${url}[)](${url}${url_final})?)?)(?:\[(?<link_text>.*?)\])?`, 29 + r`(?<=^|\s)[##](?<hashtag>(?!\uFE0F)[^${whitespace}]*[^\p{P}${whitespace}])`, // note: must filter out #123 30 + r`(?<=^|\s|[(])@(?<mention>[-a-zA-Z0-9]+([.][-a-zA-Z0-9]+)+)\b`, 31 + ].join("|"), 'gu') 32 + function process_match(match) { 33 + let g = match.groups 34 + if (g.mention!=null) { 35 + return [{text:match[0], features:[{ 36 + $type: 'app.bsky.richtext.facet#mention', 37 + did: g.mention, // hack, DID must be resolved afterwards 38 + }]}] 39 + } 40 + if (g.link!=null) { 41 + if (g.link_text) { 42 + return [{text:g.link_text, features:[{ 43 + $type: 'app.bsky.richtext.facet#link', 44 + uri: g.link, 42 45 }]}] 43 - } 44 - if (g.link!=null) { 45 - if (g.link_text) { 46 - return [{text:g.link_text, features:[{ 47 - $type: 'app.bsky.richtext.facet#link', 48 - uri: g.link, 49 - }]}] 50 - } else { 51 - return [{text:match[0], features:[{ 52 - $type: 'app.bsky.richtext.facet#link', 53 - uri: g.link, 54 - }]}] 55 - } 56 - } 57 - if (g.hashtag!=null && !/^#\d+$/.test(g.hashtag)) { 46 + } else { 58 47 return [{text:match[0], features:[{ 59 - $type: 'app.bsky.richtext.facet#tag', 60 - tag: g.link, 48 + $type: 'app.bsky.richtext.facet#link', 49 + uri: g.link, 61 50 }]}] 62 51 } 63 - return super.process_match(match) 64 52 } 53 + if (g.hashtag!=null && !/^#\d+$/.test(g.hashtag)) { 54 + return [{text:match[0], features:[{ 55 + $type: 'app.bsky.richtext.facet#tag', 56 + tag: g.link, 57 + }]}] 58 + } 59 + return [{text:match[0], features:[]}] 65 60 } 66 - 67 - let text = "abc http://example.com[test] #hello @test.com.xyz" 68 - 69 - console.log(segments) 70 - </script> 61 + export let markup1 = new MarkupParser(big_regex, process_match)
+8
test2.html
··· 1 + <!doctype html> 2 + 3 + <script type=module> 4 + import {markup1} from './parse.js' 5 + let text = "abc http://example.com[test] #hello @test.com.xyz" 6 + let x = markup1.text_to_segments(text) 7 + console.log(x) 8 + </script>
+13
test3.html
··· 1 + <!doctype html> 2 + 3 + <script type=module> 4 + import {merge_features_to_facets} from './facet.js' 5 + 6 + let spans = [ 7 + {type:"abc", start: 1, end: 5}, 8 + {type:"def", start: 3, end: 5}, 9 + {type:"zzz", start: 6, end: 10}, 10 + ] 11 + 12 + console.log(merge_features_to_facets(spans)) 13 + </script>