|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# This script replaces the contents of a section with the contents from the annotated source address or local file paths inside the DEST file. |
| 3 | +# This script replaces the contents of a section with the contents from the |
| 4 | +# annotated source address (SRC comments) inside the DEST file. |
| 5 | +# SRC comments use the format: {/* SRC: org/repo path/to/file */} |
4 | 6 |
|
5 | | -# read contents of file into memory |
6 | | -DEST="../../en/resources/contributing.md" |
| 7 | +DEST="../../src/content/pages/en/resources/contributing.mdx" |
| 8 | + |
| 9 | +# Shared content transformations |
| 10 | +transform_content() { |
| 11 | + local content="$1" |
| 12 | + local level="$2" |
| 13 | + |
| 14 | + # Keep only from first ## onward (skip title/badges) |
| 15 | + content=$(echo "$content" | sed -En '/^##|^[^#]/,$p') |
| 16 | + |
| 17 | + # Downgrade headings based on parent level |
| 18 | + content=$(echo "$content" | sed 's/^#/&'"${level:1}"'/g') |
| 19 | + |
| 20 | + # Convert GitHub callouts to Alert components |
| 21 | + content=$(echo "$content" | perl -0777 -pe ' |
| 22 | + s/> \[!(IMPORTANT|NOTE|TIP|CAUTION|WARNING)\]\s*\n((?:>.*\n)*)/ |
| 23 | + my $type = lc($1); |
| 24 | + my %map = (important => "info", note => "info", tip => "info", caution => "warning", warning => "warning"); |
| 25 | + my $alert = $map{$type} || "info"; |
| 26 | + my $body = $2; |
| 27 | + $body =~ s|^> ?||gm; |
| 28 | + "<Alert type=\"$alert\">\n\n${body}\n<\/Alert>\n" |
| 29 | + /ge') |
| 30 | + |
| 31 | + # Convert HTML comments to MDX comments |
| 32 | + content=$(echo "$content" | sed -E 's/<!--(.*)-->/\{\/\*\1\*\/\}/g') |
| 33 | + |
| 34 | + # Convert self-closing HTML tags for MDX compatibility |
| 35 | + content=$(echo "$content" | sed -E 's/<(br|hr|img)([^/]*[^/])?\s*>/<\1\2 \/>/gi') |
| 36 | + |
| 37 | + echo "$content" |
| 38 | +} |
7 | 39 |
|
8 | | -# track the header level |
9 | 40 | level='' |
10 | | -# tracks src for curl calls |
11 | 41 | src='' |
12 | | -# tracks file paths for local file reads |
13 | | -local='' |
14 | | - while IFS= read -r line; do |
15 | | - # REMOVE PREVIOUS CONTENT SECTION |
16 | | - # if src or local tags are not empty |
17 | | - if [[ -n "$src" || -n "$local" ]]; then |
18 | | - # if current line not a horitzontal rule hr |
19 | | - if [[ "$line" != "----"* ]]; then |
20 | | - # if line == level -- level is num of ## |
21 | | - if [[ "$line" == "$level"'#'* || |
22 | | - # line not a header line |
23 | | - "$line" != '#'* ]]; then |
24 | | - # skip line and rewrite over old content |
25 | | - continue |
| 42 | + |
| 43 | +while IFS= read -r line; do |
| 44 | + # Skip lines from previous SRC section (will be replaced) |
| 45 | + if [[ -n "$src" ]]; then |
| 46 | + if [[ "$line" != "----"* ]]; then |
| 47 | + if [[ "$line" == "$level"'#'* || "$line" != '#'* ]]; then |
| 48 | + continue |
26 | 49 | fi |
27 | 50 | fi |
28 | 51 | fi |
29 | 52 |
|
30 | | - # PRINT TO PAGE SECTION |
31 | 53 | src='' |
32 | | - local='' |
33 | | - # if line is a header |
| 54 | + |
| 55 | + # Track heading level |
34 | 56 | if [[ "$line" == '#'* ]]; then |
35 | | - # if header has (#id-of-link) or {#id-on-page} patterns |
36 | 57 | if [[ $line =~ (\(\#.*\))\. || "$line" =~ \{\#.*\} ]]; then |
37 | | - # isolate the matching part of line |
38 | 58 | match=${BASH_REMATCH[0]} |
39 | | - # remove match - leaving rest |
40 | 59 | rest=${line//${match}} |
41 | | - # remove any # symbols from start |
42 | 60 | title_rest=${rest##*\#} |
43 | | - # slice rest of line to get only level |
44 | 61 | level="${rest:0:$((${#rest} - ${#title_rest}))}" |
45 | 62 | else |
46 | | - # any other headers -- these before SRC/LOCAL pages anchors |
47 | | - header=${line##*\#} |
| 63 | + header=${line##*\#} |
48 | 64 | level="${line:0:$((${#line} - ${#header}))}" |
49 | 65 | fi |
50 | | - # if line is SRC anchor in read file |
51 | | - elif [[ "$line" == '<!-- SRC:'* ]]; then |
52 | | - # remove the first 10 chars |
53 | | - src=${line:10} |
54 | | - # % remove from end until after white space -- leaves src details |
55 | | - src=${src% *} |
56 | | - # if line is LOCAL anchor in read file |
57 | | - elif [[ "$line" == '<!-- LOCAL:'* ]]; then |
58 | | - # remove the first 12 chars |
59 | | - local=${line:12} |
60 | | - # % remove from end until after white space -- leave local details |
61 | | - local=${local% *} |
62 | | - # leave only path to file |
63 | | - local=${local#* } |
64 | | - fi |
65 | | - # prints line to the page |
| 66 | + # Detect SRC comment (MDX format) |
| 67 | + elif [[ "$line" == '{/* SRC:'* ]]; then |
| 68 | + src=${line:9} |
| 69 | + src=${src%% \*/*} |
| 70 | + fi |
| 71 | + |
66 | 72 | echo "$line" |
67 | | - |
68 | | - if [[ -n "$local" ]]; then |
69 | | - # cat file -- outputs full contents of file at local path |
70 | | - cat "$local" | \ |
71 | | - # remove the top 1# headers from cat'd file |
72 | | - sed -En '/^##|^[^#]/,$p' | \ |
73 | | - # remove GH MD specific tags start w '[!NOTE\] + the following line |
74 | | - sed -E '/^>\[!NOTE\]*/{N;d;}' | \ |
75 | | - # change GH specific MD IMPORTANT tags -> change into plain MD |
76 | | - sed -E 's/> \[!IMPORTANT\]/> **IMPORTANT:** /g' |
77 | | - echo |
78 | | - elif [[ -n "$src" ]]; then |
| 73 | + |
| 74 | + if [[ -n "$src" ]]; then |
79 | 75 | echo |
80 | 76 | path=${src#* } |
81 | 77 | repo=${src% *} |
82 | | - curl -s "https://raw.githubusercontent.com/${repo}/master/${path}" | \ |
83 | | - # if line is ## or not # |
84 | | - sed -En '/^##|^[^#]/,$p' | \ |
85 | | - # add additional # every header |
86 | | - sed 's/^#/&'"${level:1}"'/g' | \ |
87 | | - # format GH links when match |
88 | | - sed -E 's/(\[[^]]*\])\(([^):#]*)\)/\1(https:\/\/github.com\/'"$(sed 's/\//\\\//g' <<< "$repo")"'\/blob\/master\/\2)/g' |
| 78 | + |
| 79 | + echo "fetching $repo/$path..." >&2 |
| 80 | + RAW=$(curl -s "https://raw.githubusercontent.com/${repo}/HEAD/${path}") |
| 81 | + |
| 82 | + # Convert relative links to absolute GitHub URLs |
| 83 | + BASEURL="https://github.com/${repo}/blob/HEAD" |
| 84 | + RAW=$(echo "$RAW" | sed -E "s|\]\(([^)#/][^):]*)\)|](${BASEURL}/\1)|g") |
| 85 | + |
| 86 | + TRANSFORMED=$(transform_content "$RAW" "$level") |
| 87 | + echo "$TRANSFORMED" |
89 | 88 | echo |
90 | 89 | fi |
91 | | - # read in dest file then write back to file |
92 | 90 | done <<<"$(< $DEST)" > $DEST |
| 91 | + |
| 92 | +echo "Updated $DEST" |
0 commit comments