Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(highlightjs): Add upgrade script for highlightjs #1324

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions build/upgrade_highlightjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/bin/bash
# Run with specific branch/tag (e.g. ./upgrade_highlightjs.sh 10.6.0) or with no arguments to use master

# TODO: This list of languages should be made more dynamic based on the file extensions Preview supports, see src/lib/extensions.js
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
HIGHLIGHTJS_DIST="highlight.pack.js"
HIGHLIGHTJS_LANGUAGES="actionscript dos c cpp cmake csharp css diff erb groovy haml java javascript json less makefile ocaml php perl properties python ruby scss scala sml sql bash vim yaml"
HIGHLIGHTJS_SRC_DIR="highlightjs"
TEXT_BASE_PATH="src/third-party/text"
TEXT_STATIC_ASSETS_BRANCH=${1:-master}
TEXT_STATIC_ASSETS_VERSION=$(./build/current_version.sh)
TEXT_STATIC_ASSETS_PATH="${TEXT_BASE_PATH}/${TEXT_STATIC_ASSETS_VERSION}"

build_custom_highlightjs() {
echo "-----------------------------------------------------------------------------------"
echo "Cloning higlightjs repo at branch: $TEXT_STATIC_ASSETS_BRANCH..."
echo "-----------------------------------------------------------------------------------"
rm -rf ${HIGHLIGHTJS_SRC_DIR}
git clone git@github.com:highlightjs/highlight.js.git --depth 1 --single-branch --branch ${TEXT_STATIC_ASSETS_BRANCH} ${HIGHLIGHTJS_SRC_DIR} || return 1

echo "-----------------------------------------------------------------------------------"
echo "Building higlightjs for browser for languages ${HIGHLIGHTJS_LANGUAGES}..."
echo "-----------------------------------------------------------------------------------"
# Use subshell to execute the custom build to avoid having to keep track of the current working directory
(cd ${HIGHLIGHTJS_SRC_DIR} && yarn install --frozen-lockfile && node tools/build -t browser ${HIGHLIGHTJS_LANGUAGES}) || return 1
}

cleanup_custom_highlightjs() {
echo "-----------------------------------------------------------------------------------"
echo "Cleaning up highlightjs repo"
echo "-----------------------------------------------------------------------------------"
rm -rf ${HIGHLIGHTJS_SRC_DIR}
}

prepare_target_directory() {
echo "-----------------------------------------------------------------------------------"
echo "Creating target directory at $TEXT_STATIC_ASSETS_PATH..."
echo "-----------------------------------------------------------------------------------"

rm -rf ${TEXT_STATIC_ASSETS_PATH}
TEXT_CURRENT_ASSETS_VERSIONS=`ls ${TEXT_BASE_PATH} | sort -t "." -k1,1n -k2,2n -k3,3n | tail -1`

echo "Using base version from $TEXT_CURRENT_ASSETS_VERSIONS"
mkdir -v ${TEXT_STATIC_ASSETS_PATH}
cp -pv ${TEXT_BASE_PATH}/${TEXT_CURRENT_ASSETS_VERSIONS}/* ${TEXT_STATIC_ASSETS_PATH}/
}

process_highlightjs_assets() {
echo "-----------------------------------------------------------------------------------"
echo "Copying relevant files to third-party directory..."
echo "-----------------------------------------------------------------------------------"
cp -v ${HIGHLIGHTJS_SRC_DIR}/build/${HIGHLIGHTJS_DIST} ${TEXT_STATIC_ASSETS_PATH}/highlight.min.js || return 1
cp -v ${HIGHLIGHTJS_SRC_DIR}/src/styles/github.css ${TEXT_STATIC_ASSETS_PATH} || return 1

echo "-----------------------------------------------------------------------------------"
echo "Minifying github.css with cssnano"
echo "-----------------------------------------------------------------------------------"
./node_modules/.bin/cssnano ${TEXT_STATIC_ASSETS_PATH}/github.css ${TEXT_STATIC_ASSETS_PATH}/github.min.css || return 1
}

upgrade_highlightjs() {
echo "Upgrading highlight.js to $TEXT_STATIC_ASSETS_BRANCH";

# Prepare target directory under third-party/text
prepare_target_directory || return 1

# Build highlightjs from the specified version
build_custom_highlightjs || return 1

# Copy over built assets to target directory
process_highlightjs_assets || return 1

# Cleanup highlightjs
cleanup_custom_highlightjs || return 1

echo "-----------------------------------------------------------------------------------"
echo "Successfully upgraded highlightjs! 🚀"
echo "-----------------------------------------------------------------------------------"
}

# Execute this entire script
if ! upgrade_highlightjs; then
echo "----------------------------------------------------------------------"
echo "Error while upgrading highlightjs to latest version!"
echo "----------------------------------------------------------------------"

echo "----------------------------------------------------------------------"
echo "Clean workspace by deleting ${TEXT_STATIC_ASSETS_PATH} and ${HIGHLIGHTJS_SRC_DIR}"
echo "----------------------------------------------------------------------"
exit 1
fi
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"test:e2e:open": "npm-run-all -p -r start:dev cy:open",
"test:watch": "yarn test --watch",
"upgrade:annotations": "./build/upgrade_annotations.sh",
"upgrade:highlightjs": "./build/upgrade_highlightjs.sh",
"upgrade:pdfjs": "./build/upgrade_pdfjs.sh"
},
"browserslist": [
Expand Down
3 changes: 3 additions & 0 deletions src/lib/extensions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// If changes are made to this file to support more extensions that are "highlightable", then make sure to
// check if we need to upgrade the highlightjs custom build to add language grammar for that new extension language.
// See build/upgrade_highlightjs.sh
export const NON_CODE_EXTENSIONS = ['csv', 'log', 'md', 'tsv', 'txt'];

export const HTML_EXTENSIONS = ['htm', 'html', 'xhtml', 'xml', 'xsd', 'xsl']; // These types do not have an appropriate extracted text representation for preview
Expand Down