{"componentChunkName":"component---src-pages-author-author-yaml-id-js","path":"/author/lucius-yu/","result":{"data":{"allMarkdownRemark":{"edges":[{"node":{"id":"30716803-ba80-5a62-868d-6b7fa44ebe12","html":"<h3 id=\"delete-a-node\" style=\"position:relative;\"><a href=\"#delete-a-node\" aria-label=\"delete a node permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Delete A Node</h3>\n<p><a href=\"https://leetcode.com/problems/delete-node-in-a-linked-list/\">Click to View the Orginal Question</a></p>\n<p>To remove the given node，which means whoever is asking the value for this node should get the value of the next node. Plus, the next node of this removing one should be the next next node.</p>\n<p>In diagram, with the following node list, we want to remove node 2.</p>\n<p>1 -> 2 -> 3 -> 4</p>\n<p>We update original Node 2 to Node 3 and skip the original Node 3, which gives out the list after removing node 2.</p>\n<p>1 -> 3 ------> 4</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* Definition for singly-linked list.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* function ListNode(val) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* this.val = val;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* this.next = null;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{ListNode}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">node</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@return</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{void}</span><span class=\"mtk3\"> Do not return anything, modify node in-place instead.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk11\">deleteNode</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">node</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">node</span><span class=\"mtk1\">.</span><span class=\"mtk12\">val</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">node</span><span class=\"mtk1\">.</span><span class=\"mtk12\">next</span><span class=\"mtk1\">.</span><span class=\"mtk12\">val</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk12\">node</span><span class=\"mtk1\">.</span><span class=\"mtk12\">next</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">node</span><span class=\"mtk1\">.</span><span class=\"mtk12\">next</span><span class=\"mtk1\">.</span><span class=\"mtk12\">next</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span></code></pre>\n<h3 id=\"is-same-tree\" style=\"position:relative;\"><a href=\"#is-same-tree\" aria-label=\"is same tree permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Is Same Tree</h3>\n<p><a href=\"https://leetcode.com/problems/same-tree/\">Click to View the Original Question</a></p>\n<p><strong>Note</strong>: My solution is not efficient enough based on the performance, but I think it's fairly clean.</p>\n<p>I use recursion to solve the problem, first to define the basic cases, and recursively loop through both trees from left to right. If it hits one <code>false</code>, the whole result will be <code>false</code>.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* Definition for a binary tree node.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* function TreeNode(val) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* this.val = val;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* this.left = this.right = null;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{TreeNode}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">p</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{TreeNode}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">q</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@return</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{boolean}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk11\">isSameTree</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">p</span><span class=\"mtk1\">, </span><span class=\"mtk12\">q</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\">( </span><span class=\"mtk12\">p</span><span class=\"mtk1\"> === </span><span class=\"mtk12\">q</span><span class=\"mtk1\"> ) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">true</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\">(!</span><span class=\"mtk12\">p</span><span class=\"mtk1\"> || !</span><span class=\"mtk12\">q</span><span class=\"mtk1\"> || </span><span class=\"mtk12\">p</span><span class=\"mtk1\">.</span><span class=\"mtk12\">val</span><span class=\"mtk1\"> !== </span><span class=\"mtk12\">q</span><span class=\"mtk1\">.</span><span class=\"mtk12\">val</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk4\">false</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk11\">isSameTree</span><span class=\"mtk1\">(</span><span class=\"mtk12\">p</span><span class=\"mtk1\">.</span><span class=\"mtk12\">left</span><span class=\"mtk1\">, </span><span class=\"mtk12\">q</span><span class=\"mtk1\">.</span><span class=\"mtk12\">left</span><span class=\"mtk1\">) ? </span><span class=\"mtk11\">isSameTree</span><span class=\"mtk1\">(</span><span class=\"mtk12\">p</span><span class=\"mtk1\">.</span><span class=\"mtk12\">right</span><span class=\"mtk1\">, </span><span class=\"mtk12\">q</span><span class=\"mtk1\">.</span><span class=\"mtk12\">right</span><span class=\"mtk1\">) : </span><span class=\"mtk4\">false</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span></code></pre>\n<h3 id=\"move-zeroes\" style=\"position:relative;\"><a href=\"#move-zeroes\" aria-label=\"move zeroes permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Move Zeroes</h3>\n<p><a href=\"https://leetcode.com/problems/move-zeroes/\">Click to View the Original Question</a></p>\n<p>Here I have create two indices one called <code>z</code> to index the position for zeroes, another is <code>i</code> to loop through the nums array. They both increment by one when they see a non-zero value, but when it is zero, z stays at position and get ready to swap the position with next non-zero value <code>i</code> meets. By keep doing it, it will fill a consecutive row of zeroes, till the end.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"javascript\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{number[]}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">nums</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@return</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{void}</span><span class=\"mtk3\"> Do not return anything, modify nums in-place instead.</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk11\">moveZeroes</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">nums</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">z</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">for</span><span class=\"mtk1\">(</span><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> = </span><span class=\"mtk7\">0</span><span class=\"mtk1\">; </span><span class=\"mtk12\">i</span><span class=\"mtk1\"> </span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"title":"Delete a Node, Is Same Tree, Move Zeroes","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"December 22, 2015","updated_date":null,"tags":["Data Structure","JavaScript","Linked List"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/4dbfe94040c20cde6c3a395142bd3009/630fb/code-js-300x300.png","srcSet":"/static/4dbfe94040c20cde6c3a395142bd3009/69585/code-js-300x300.png 200w,\n/static/4dbfe94040c20cde6c3a395142bd3009/630fb/code-js-300x300.png 300w","sizes":"(max-width: 300px) 100vw, 300px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/delete-a-node-is-same-tree-move-zeroes/"}}},{"node":{"id":"16b81bfc-4e54-516f-b548-9d83b77ea160","html":"<h3 id=\"1-nim-game\" style=\"position:relative;\"><a href=\"#1-nim-game\" aria-label=\"1 nim game permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1. Nim Game</h3>\n<p><a href=\"https://leetcode.com/problems/nim-game/\">Click to View Original Question</a></p>\n<p>This question is very easy once you figure out the trick behind the game.\nSince each time you can only pick 3 stones at most, if there are 4 stones originally on the table, no matter how many you take ... your hopefully non-stupid friend can easily beat you. The same logic also applies when the number of stones is more than 4.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{number}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">n</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@return</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{boolean}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk11\">canWinNim</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">n</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">n</span><span class=\"mtk1\"> % </span><span class=\"mtk7\">4</span><span class=\"mtk1\"> === </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> ? </span><span class=\"mtk4\">false</span><span class=\"mtk1\"> : </span><span class=\"mtk4\">true</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span></code></pre>\n<h3 id=\"\" style=\"position:relative;\"><a href=\"#\" aria-label=\" permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h3>\n<p>2. Add Digits</p>\n<p><a href=\"https://leetcode.com/problems/add-digits/\">Click to View Original Question</a></p>\n<p>This is a tricky question, the key is trying to find the repetitive pattern of the Digits Sum. If you can't get the pattern by observation then we can do some math to find the pattern.</p>\n<p>So assume we have a 4 digit number \"abcd\" or 1234, and we want to find out the sum of the digits which in expression is:</p>\n<p>var digitSum = a + b + c + d</p>\n<p>We still can't figure out the answer from this alone, so we need to find something else to help us to get the value. Then we can consider the actual value of \"abcd\" = 1000a + 100b + 10c + d.</p>\n<p>- If you're familiar with Ring Theory (pretty sure it's Ring Theory.. I could be wrong..) then perfect! Under Ring 9, the actual value of abcd === a + b + c + d.\n- And if you don't know about Ring Theory, think of this as getting the remainder of \"abcd\" divided by 9</p>\n<p>> (1000a + 100b + 10c + d) % 9 => (999a + a + 99b + b + 9c + c + d) % 9 => a + b + c + d</p>\n<p>So whatever the reminder is equals to the sum of the digits and that is more than enough information to solve the question.</p>\n<p><strong>Be careful with the number 0 !!</strong></p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk3\">/**</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@param</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{number}</span><span class=\"mtk3\"> </span><span class=\"mtk12\">num</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">* </span><span class=\"mtk4\">@return</span><span class=\"mtk3\"> </span><span class=\"mtk10\">{number}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk3\">*/</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk4\">var</span><span class=\"mtk1\"> </span><span class=\"mtk11\">addDigits</span><span class=\"mtk1\"> = </span><span class=\"mtk4\">function</span><span class=\"mtk1\">(</span><span class=\"mtk12\">num</span><span class=\"mtk1\">) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (</span><span class=\"mtk12\">num</span><span class=\"mtk1\"> === </span><span class=\"mtk7\">0</span><span class=\"mtk1\">) </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">num</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">num</span><span class=\"mtk1\"> % </span><span class=\"mtk7\">9</span><span class=\"mtk1\"> === </span><span class=\"mtk7\">0</span><span class=\"mtk1\"> ? </span><span class=\"mtk7\">9</span><span class=\"mtk1\"> : </span><span class=\"mtk12\">num</span><span class=\"mtk1\"> % </span><span class=\"mtk7\">9</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">};</span></span></code></pre>\n<h3 id=\"-1\" style=\"position:relative;\"><a href=\"#-1\" aria-label=\" 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h3>\n<p>3. Maximum Depth of Binary Tree</p>\n<p><a href=\"https://leetcode.com/problems/maximum-depth-of-binary-tree/\">Click to View Original Question</a></p>\n<p>This is very typical of recursion problems, and recursion is always hard to explain in words</p>\n<p>(╯‵□′)╯︵┻━┻</p>\n<p>Regardless I will try my very best to describe it.</p>\n<p>1. You have a tree node, and check if it is an empty tree, if so return 0\n2. If it is not empty, use recursion to find the value\n3. The recursion will start with the left node of root, and iterate through the tree, and keep checking the depth of each node till reaching out to the leaves. Each node (small root) records its maximum depth value by a record count called \"\"currentLength\"\".\n4. Until the whole tree is traversed, the root will compare its leftLength and rightLength and return the answer!</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">rightLength</span><span class=\"mtk1\"> ? </span><span class=\"mtk12\">leftLength</span><span class=\"mtk1\"> : </span><span class=\"mtk12\">rightLength</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">return</span><span class=\"mtk1\"> </span><span class=\"mtk12\">rootLength</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}--&gt;</span></span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk10 { color: #4EC9B0; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n</style>","frontmatter":{"title":"Nim Game, Add Digits, Maximum Depth of Binary Tree","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"November 24, 2015","updated_date":null,"tags":["Nim","JavaScript"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/e9162a5f9c634d0d0932d5bc4a18b24e/6d161/nim-blog-150x150.png","srcSet":"/static/e9162a5f9c634d0d0932d5bc4a18b24e/6d161/nim-blog-150x150.png 150w","sizes":"(max-width: 150px) 100vw, 150px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/nim-game-add-digits-maximum-depth-of-binary-tree/"}}},{"node":{"id":"e3d92d09-d0ed-5606-92bd-bd396d1a063d","html":"<p>So last week while I was setting the analyzers on ElasticSearch settings for Email field, it took me some good time to find the perfect custom analyzer for my purpose, so I feel it might be useful to share this with someone who needs it.</p>\n<blockquote>\n<p>When a document is indexed, its individual fields are subject to the analyzing and tokenizing filters that can transform and normalize the data in the fields. For example — removing blank spaces, removing html code, stemming, removing a particular character and replacing it with another. At indexing time as well as at query time you may need to do some of the above or similiar operations. For example, you might perform a Soundex transformation (a type of phonic hashing) on a string to enable a search based upon the word and upon its 'sound-alikes'.</p>\n</blockquote>\n<p>Let me briefly explain what I am trying to do with my Email field. The email addresses will be stored as a comma separated string with potentially multiple email addresses, and what I am expecting is after the to search to retrieve the matching results. Besides the exact match like search for <code>\"john.doe@gmail.com\"</code>, I also want to be able to search for <code>\"john.doe\"</code> or <code>\"gmail.com\"</code> to retrieve the relative information.</p>\n<p>Here is the full schema for it:</p>\n<ol>\n<li>{\"email\": \"john.doe@gmail.com\"}</li>\n<li>{\"email\": \"john.doe@gmail.com, john.doe@outlook.com\"}</li>\n<li>{\"email\": \"hello-john.doe@outlook.com\"}</li>\n<li>{\"email\": \"john.doe@outlook.com\"}</li>\n<li>{\"email\": \"john@yahoo.com\"}</li>\n</ol>\n<p>And the search schema is:<br>\n[Search > Response]<br>\n\"john.doe@gmail.com\" > 1,2<br>\n\"john.doe@outlook.com\" > 2,4<br>\n\"john@yahoo.com\" > 5<br>\n\"john.doe\" > 1,2,3,4<br>\n\"john\" > 1,2,3,4,5<br>\n\"gmail.com\" > 1,2<br>\n\"outlook.com\" > 2,3,4</p>\n<p>If you decide to keep reading, then it means you have the same needs as I did so I hope the following solution can actually help.<br>\nSo... In the official documentation they did have a good solution but it was not easy to find, at least for me, by using regular expression:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">{</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">   </span><span class=\"mtk8\">&quot;settings&quot;</span><span class=\"mtk1\"> : {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      </span><span class=\"mtk8\">&quot;analysis&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">         </span><span class=\"mtk8\">&quot;filter&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk8\">&quot;email&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               </span><span class=\"mtk8\">&quot;type&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;pattern_capture&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               </span><span class=\"mtk8\">&quot;preserve_original&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> </span><span class=\"mtk7\">1</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               </span><span class=\"mtk8\">&quot;patterns&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> [</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  </span><span class=\"mtk8\">&quot;([^@]+)&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  </span><span class=\"mtk8\">&quot;(</span><span class=\"mtk6\">\\p</span><span class=\"mtk8\">{L}+)&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  </span><span class=\"mtk8\">&quot;(</span><span class=\"mtk6\">\\d</span><span class=\"mtk8\">+)&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">                  </span><span class=\"mtk8\">&quot;@(.+)&quot;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               ]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">         },</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">         </span><span class=\"mtk8\">&quot;analyzer&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk8\">&quot;email&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               </span><span class=\"mtk8\">&quot;tokenizer&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;uax_url_email&quot;</span><span class=\"mtk1\">,</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">               </span><span class=\"mtk8\">&quot;filter&quot;</span><span class=\"mtk12\"> :</span><span class=\"mtk1\"> [ </span><span class=\"mtk8\">&quot;email&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;lowercase&quot;</span><span class=\"mtk1\">,  </span><span class=\"mtk8\">&quot;unique&quot;</span><span class=\"mtk1\"> ]</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">         }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">      }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">   }</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span></code></pre>\n<p>This setting will perfectly address the issues from above, and do not generate way to much tokens in your ES cluster.<br>\nAt the end I want to share two good gadgets that I found very helpful with ES analyzers and regular expression.</p>\n<ol>\n<li><strong>Inquisitor:</strong><br>\nThis Elastic Search tool allows you to quickly apply different analyzers, tokenizers and filters to your input, fork it here:<br>\n<a href=\"https://github.com/polyfractal/elasticsearch-inquisitor\">Elasticsearch Inquisitor</a></li>\n<li><strong>regex 101:</strong>\nTo quickly test out your regular expression, try this link: <a href=\"https://regex101.com/\">regex101</a></li>\n</ol>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk7 { color: #B5CEA8; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"title":"ElasticSearch Analyzers for Emails","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"October 15, 2015","updated_date":null,"tags":["Elastic Search","Analyzers"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.9230769230769231,"src":"/static/db4c939fb2ba73abdc984760c0082348/ee604/elastic-search.png","srcSet":"/static/db4c939fb2ba73abdc984760c0082348/69585/elastic-search.png 200w,\n/static/db4c939fb2ba73abdc984760c0082348/497c6/elastic-search.png 400w,\n/static/db4c939fb2ba73abdc984760c0082348/ee604/elastic-search.png 800w,\n/static/db4c939fb2ba73abdc984760c0082348/f3583/elastic-search.png 1200w,\n/static/db4c939fb2ba73abdc984760c0082348/5707d/elastic-search.png 1600w,\n/static/db4c939fb2ba73abdc984760c0082348/a16c9/elastic-search.png 3422w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/elastic-search-analyzers-for-emails/"}}},{"node":{"id":"e7d63795-43e6-5632-be06-5bba253460ed","html":"<p>Recently, I was working on an implementation to build an embedded app on Shopify with PHP. I realized that the 3rd party PHP SDK recommended by Shopify called \"phpish\" does not support the feature to \"PUT\" assets into your shopify shop's theme. The link to \"phpish\" git repository can be found  <a href=\"https://github.com/phpish/shopify\">here</a>, I'd like to thank these guys for their great work, it saved me lots of time to get the implementation on the right track.</p>\n<p>So if you want to do a PUT API call to Shopify web service, you can do it in raw PHP by customizing a CURL request, this is not very hard. But prior to make the Asset API call, first you need to retrieve the currently activated theme ID by calling the Shopify Theme API call, this part can be done easily with the help of phpish.</p>\n<p>Then it is the time to customize your CURL request to do the PUT API call, remember phpish stores our information such as Oauth Token and Shop into the $_SESSION, we can leverage on that or manually fill in the correct information</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"php\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!-- </span><span class=\"mtk11\">array</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;key&quot;</span><span class=\"mtk1\"> =&gt; </span><span class=\"mtk8\">&quot;snippets/put-asset.liquid&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;value&quot;</span><span class=\"mtk1\"> =&gt; </span><span class=\"mtk8\">&quot;this is a test to put assets&quot;</span><span class=\"mtk1\">));</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$ch</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">curl_init</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$_SESSION</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&#39;shop&#39;</span><span class=\"mtk1\">].</span><span class=\"mtk8\">&quot;/admin/themes/</span><span class=\"mtk12\">$theme_id</span><span class=\"mtk8\">/assets.json&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_RETURNTRANSFER, </span><span class=\"mtk4\">true</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_CUSTOMREQUEST, </span><span class=\"mtk8\">&quot;PUT&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">array</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\">[] = </span><span class=\"mtk8\">&quot;X-Shopify-Access-Token: &quot;</span><span class=\"mtk1\">.</span><span class=\"mtk11\">echo</span><span class=\"mtk1\"> </span><span class=\"mtk12\">$_SESSION</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&#39;oauth_token&#39;</span><span class=\"mtk1\">];</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\">[] = </span><span class=\"mtk8\">&quot;Content-Type: application/json&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">( </span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_HTTPHEADER, </span><span class=\"mtk12\">$headers</span><span class=\"mtk1\"> );</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">( </span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_POSTFIELDS, </span><span class=\"mtk11\">json_encode</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$data</span><span class=\"mtk1\">));</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$response</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">curl_exec</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">echo</span><span class=\"mtk1\"> </span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">echo</span><span class=\"mtk1\"> </span><span class=\"mtk11\">curl_error</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">).</span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">var_dump</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$response</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_close</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">?&gt;--&gt;</span></span></code></pre>\n<p>Let me briefly explain this snippet, so first you define an array with key and value, key maps to the file name that you want to create, and value is the actual code or asset you want to pass in. Then you create a curl request with method \"PUT\" like all the other requests</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"php\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">$ch</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">curl_init</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$_SESSION</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&#39;shop&#39;</span><span class=\"mtk1\">].</span><span class=\"mtk8\">&quot;/admin/themes/</span><span class=\"mtk12\">$theme_id</span><span class=\"mtk8\">/assets.json&quot;</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_RETURNTRANSFER, </span><span class=\"mtk4\">true</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_CUSTOMREQUEST, </span><span class=\"mtk8\">&quot;PUT&quot;</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>Next is something special for Shopify, to make an API call to Shopify you have to specify Oauth_Token in your request to tell Shopify who you are, and so does Content-Type.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"php\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">array</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\">[] = </span><span class=\"mtk8\">&quot;X-Shopify-Access-Token: &quot;</span><span class=\"mtk1\">.</span><span class=\"mtk11\">echo</span><span class=\"mtk1\"> </span><span class=\"mtk12\">$_SESSION</span><span class=\"mtk1\">[</span><span class=\"mtk8\">&#39;oauth_token&#39;</span><span class=\"mtk1\">];</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$headers</span><span class=\"mtk1\">[] = </span><span class=\"mtk8\">&quot;Content-Type: application/json&quot;</span><span class=\"mtk1\">;</span></span></code></pre>\n<p>Last but not least, json encode your data array into JSON format and send the request.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"php\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk11\">curl_setopt</span><span class=\"mtk1\">( </span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">, CURLOPT_POSTFIELDS, </span><span class=\"mtk11\">json_encode</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$data</span><span class=\"mtk1\">));</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">$response</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">curl_exec</span><span class=\"mtk1\">(</span><span class=\"mtk12\">$ch</span><span class=\"mtk1\">);</span></span></code></pre>\n<p>The last part is just to catch the returned response or show the error message from your CURL request if something is going wrong.</p>\n<p>Hope this can help someone, happy coding.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n</style>","frontmatter":{"title":"Shopify Embedded App","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"June 02, 2015","updated_date":null,"tags":["Shopify","PHP"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.8867924528301887,"src":"/static/4872b27152104126f3a84ce3bb3dc334/2b011/shopify.png","srcSet":"/static/4872b27152104126f3a84ce3bb3dc334/69585/shopify.png 200w,\n/static/4872b27152104126f3a84ce3bb3dc334/2b011/shopify.png 308w","sizes":"(max-width: 308px) 100vw, 308px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/shopify-embedded-app/"}}},{"node":{"id":"63958828-44ab-574e-978a-1602db73011f","html":"<p><strong>Overview</strong></p>\n<p>Filter Portfolio can be very useful for websites, especially when there is a lot of images you want to show to users. It is always nice to keep it into different categories and allow your user to play with them.</p>\n<p>But adding a tag and a div on hundreds of images is definitely not fun. Our goal today is to use PHP to generate the Filter Portfolio instead of manually adding them one by one.</p>\n<p>The logic is like this, first we have a folder to contain all the portfolio images. It could be like this:</p>\n<p>\"assets->img->logos->businesses\"</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 670px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 58.00000000000001%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsTAAALEwEAmpwYAAABVUlEQVQoz8WRz04CMRDG9+V9AA++hheNBwXUSDSCEdEAahRkYbfl32a72+522g5jV4zRxAPx4nea9ptf5ps20HKKTlmHWnFTLpxD0AJKbi0YA1pF1siN62BVuWUKKkaEoiiCbHGx5CERlaItlk+4JqeHfFQzxhBBwuoi5d6F/CYOO4iEMEzYtS4KAAj4y+H1aZOslfPGbNK3jlze7TT2UmGJbOto57bd9LCcn7Qu6xbJqd6oV5vFmXUmYOF9FDEAK1bDcPwqFZDL2GSQy5II2fhuGkYeTvhjFA6zvECT8ukgz1U1WYp5JhLEtYVcF6lzzrci4qbw2hQOCcBY6+P4lNb3a62D1G+VvFbBVleLuOupD2RNn/oqvqu6rOD++W7v4dgfps/7Z7WDsoTKXP/K/FAFs7cWY7FPVYgXHo00GNpOFezfXEn18TEkc7nZaluY/qp/hd8Bhl60n/dN47sAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"folder-structure\"\n        title=\"folder-structure\"\n        src=\"/static/d4a3eedb9dab04f462db04bf921452f6/d67fd/folder-structure.png\"\n        srcset=\"/static/d4a3eedb9dab04f462db04bf921452f6/a6d36/folder-structure.png 650w,\n/static/d4a3eedb9dab04f462db04bf921452f6/d67fd/folder-structure.png 670w\"\n        sizes=\"(max-width: 670px) 100vw, 670px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>And in the \"businesses\" folder, we have some sub-folders to put all the images in different categories, like this:  </p>\n<p>Each sub-folder contains different images in that category, and our goal is to use those sub-folders name as the filters, and the images under each category will be added automatically. Let's do it.</p>\n<ul>\n<li>First we need to implement a filter portfolio.</li>\n</ul>\n<p>I know people hate this, but we are going to use a little pre-built javascript file called MixitUp, I promise it is a very easy but powerful tool to use, see a <a href=\"http://www.jqueryrain.com/?URGti1_4\">demo here</a>.</p>\n<p>There is already very comprehensive tutorial on how to install that plugin  in their <a href=\"https://github.com/patrickkunka/mixitup\">github</a> repository, you can follow either of these.</p>\n<ol>\n<li><strong>Download the javascript file from github</strong></li>\n</ol>\n<p>Click this link <a href=\"https://github.com/patrickkunka/mixitup/tree/v3/src\">git -> src</a> to open the repository for the javascript file, download and save it in your asset folder.</p>\n<ol>\n<li><strong>Import js and css</strong></li>\n</ol>\n<p>You can do this right before the closing body tag. You probably have Jquery installed already, but just in case you don't.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<p>It is recommended to link your Mixitup function while your document is ready, so add this after the importing lines.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk12\">document</span><span class=\"mtk1\">).</span><span class=\"mtk11\">ready</span><span class=\"mtk1\">(</span><span class=\"mtk4\">function</span><span class=\"mtk1\">(){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk11\">jQuery</span><span class=\"mtk1\">(</span><span class=\"mtk4\">function</span><span class=\"mtk1\">(){</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk4\">function</span><span class=\"mtk1\">() {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk11\">$</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&#39;#Container&#39;</span><span class=\"mtk1\">).</span><span class=\"mtk11\">mixItUp</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        });</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    });</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">});</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<ol>\n<li><strong>Add the code</strong></li>\n</ol>\n<p>Here is where the fun begins! I am not going to do any stylings in this tutorial, since you can always customize it yourself. Okay, first of first, add a div as a container of your filter portfolio section, so:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!----&gt;</span></span></code></pre>\n<p>Next, we need to add those clickable filter buttons to the div we just created. We can add them programmatically, but to be honest, it is not that bad to do it manually.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!-- </span><span class=\"mtk12\">All</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Eateries</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Cafes</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Bars</span><span class=\"mtk1\">, </span><span class=\"mtk12\">Clubs</span><span class=\"mtk1\"> & </span><span class=\"mtk12\">Lounges</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Lifestyle</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Health</span><span class=\"mtk1\"> & </span><span class=\"mtk12\">Fitness</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Fashion</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Beauty</span><span class=\"mtk1\"> & </span><span class=\"mtk12\">Spas</span><span class=\"mtk1\"> </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\"> </span><span class=\"mtk12\">Entertainment</span><span class=\"mtk1\"> --&gt;</span></span></code></pre>\n<p>Please note, for these <code>&#x3C;a></code> tags, you need to follow the patterns of MixitUp. Basically in class attribute you need to specify \"filter\" for it, and in data-filter, you need to tell which category it is. Next we are going to write some PHP code, to iterate through the destination folder, and process the files within it.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"php\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">valid</span><span class=\"mtk1\">()) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span><span class=\"mtk15\">if</span><span class=\"mtk1\"> (!</span><span class=\"mtk12\">$it</span><span class=\"mtk1\">-&gt;</span><span class=\"mtk11\">isDot</span><span class=\"mtk1\">()) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">$subject</span><span class=\"mtk1\"> = </span><span class=\"mtk12\">$it</span><span class=\"mtk1\">-&gt;</span><span class=\"mtk11\">getSubPathName</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">// Mac OS automatically creates .DS_store file to store metadata</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk3\">// The following regex is used to ignore those files</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk12\">$pattern</span><span class=\"mtk1\"> = </span><span class=\"mtk5\">&#39;/</span><span class=\"mtk6\">\\\\.</span><span class=\"mtk5\">DS/&#39;</span><span class=\"mtk1\">;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk11\">preg_match</span><span class=\"mtk1\">( </span><span class=\"mtk12\">$pattern</span><span class=\"mtk1\">, </span><span class=\"mtk12\">$subject</span><span class=\"mtk1\">, </span><span class=\"mtk12\">$match</span><span class=\"mtk1\"> );</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        </span><span class=\"mtk15\">if</span><span class=\"mtk1\">( !</span><span class=\"mtk12\">$match</span><span class=\"mtk1\"> ) {</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">            </span><span class=\"mtk12\">$file_path_name</span><span class=\"mtk1\"> = </span><span class=\"mtk11\">str_replace</span><span class=\"mtk1\">(</span><span class=\"mtk8\">&quot;</span><span class=\"mtk6\">\\\\</span><span class=\"mtk8\">&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk8\">&quot;/&quot;</span><span class=\"mtk1\">, </span><span class=\"mtk12\">$subject</span><span class=\"mtk1\">);</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">?&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    &lt;div </span><span class=\"mtk4\">class</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;mix getSubPath(); ?&gt; col-md-3 &quot;</span><span class=\"mtk1\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">        &lt;img src=</span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\"> alt=</span><span class=\"mtk8\">&quot;&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk4\">class</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;business-logo-img&quot;</span><span class=\"mtk1\">/&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">    </span></span>\n<span class=\"grvsc-line\"><span class=\"mtk11\">next</span><span class=\"mtk1\">();</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">}</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">?&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<p>The code is pretty straightforward, just want to label out a couple of things.</p>\n<p>First, we specified which folders we are working with and then we used a built-in recursive iterators to cycle through the folder.</p>\n<p>For each item we found in every sub-folder, $it->getSubPath will return the sub-folder name and $it->getSubPathName will return the name of the file. Create a div for each image, and play with those returned parameters to fit them into the pattern of MixitUp.</p>\n<p>The $it->isDot() function and regular expressions are used to filter out some system hidden files such as \".DS_Store\" and others, since we do not want to them to be shown as images.</p>\n<p>Easy, right? If you like this post, click share and help people who's having this kind of issue.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk11 { color: #DCDCAA; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk4 { color: #569CD6; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n  .dark-default-dark .mtk15 { color: #C586C0; }\n  .dark-default-dark .mtk3 { color: #6A9955; }\n  .dark-default-dark .mtk5 { color: #D16969; }\n  .dark-default-dark .mtk6 { color: #D7BA7D; }\n</style>","frontmatter":{"title":"Use PHP to generate filter portfolio","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"May 19, 2015","updated_date":null,"tags":["PHP"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/1ea6f04417f886989e6dc30837c305f5/7d145/php-filter-portfolio.png","srcSet":"/static/1ea6f04417f886989e6dc30837c305f5/69585/php-filter-portfolio.png 200w,\n/static/1ea6f04417f886989e6dc30837c305f5/497c6/php-filter-portfolio.png 400w,\n/static/1ea6f04417f886989e6dc30837c305f5/7d145/php-filter-portfolio.png 610w","sizes":"(max-width: 610px) 100vw, 610px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/use-php-to-generate-filter-portfolio/"}}},{"node":{"id":"61b4b624-d0cb-5524-8387-f4b9bf1951a2","html":"<p>It is important nowadays to allow your user to sign in to your website with their social network account. It provides a better experience for your user and also lets you obtain more information about your user. The most welcomed social platforms are Facebook, Twitter, LinkedIn and Google. In this article, we will cover how to integrate LinkedIn social login authentication into your website.</p>\n<h2> Create a LinkedIn App </h2>\n<p>Creating an app for the social platform is always the first step, no matter which platform you are working with. In this case, the created app will service the LinkedIn social login. Basically, you are creating a gate to let your user go through this gate and access the service, and in this gate, you can specify the permissions and the preferences you want to grant to your user. You can refer to the instructions <a href=\"https://www.loginradius.com/docs/api/v2/admin-console/social-provider/app-reviews/linkedin-app-review/\">here</a> for clear instructions on how to create a LinkedIn social login app.</p>\n<h2> Set your App </h2>\n<p>After creating the app, you need to set the \"JavaScript API domain\" field for your app.  In that field, fill in your website URL. Here we have used <code>http://localhost</code> for this article.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 56.769230769230774%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAIAAADwazoUAAAACXBIWXMAAAsTAAALEwEAmpwYAAABLElEQVQoz3VSS26FMAzk/teo1EVP0mWX1eu2lXh8QsjPSQih40AorfoGsAL22GObJqXFGGOtE0JIKYnIFVh8co4KQgieQWzKMca4bVuzLIvRxlk3FVyZ5cAZ/E6l4xBOcs55T4woVEZ9pBiFGMdhLMDrfEDhQRjiDzIeyEYQHCCD03f9JIQ2DARB2vYADeRYZ6mo0lqj8jD0yFLF881zIAd1+QImBz8L8azUi1JP1ryp2aEHONaKlNJZ6so8ZJ9AXGWwyZktZP8p+FMZPmsslgV5aLu93/uhB7quk3LSBVR3VjZAaMUXdQ2q8Q5wkV9jSIEwJ4TwuLQuv4DBeP8fWG2G7edk31sZ05p/B8F7SM0Z01fWO19XtdUebu30+vG1b/LRejAKH5e48BS/AQlsfxPcUy0zAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"luciusblog2-1\"\n        title=\"luciusblog2-1\"\n        src=\"/static/357c8cb91c4575f5f4f4947f81c6ee60/e5715/luciusblog2-1.png\"\n        srcset=\"/static/357c8cb91c4575f5f4f4947f81c6ee60/a6d36/luciusblog2-1.png 650w,\n/static/357c8cb91c4575f5f4f4947f81c6ee60/e5715/luciusblog2-1.png 768w,\n/static/357c8cb91c4575f5f4f4947f81c6ee60/36c33/luciusblog2-1.png 946w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<h2>Implement the script in your html file</h2>\n<p>Now you are ready to code! First, put this JavaScript code snippet inside your head tag:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--&lt;</span><span class=\"mtk12\">script</span><span class=\"mtk1\"> </span><span class=\"mtk12\">type</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;text/javascript&quot;</span><span class=\"mtk1\"> </span><span class=\"mtk12\">src</span><span class=\"mtk1\">=</span><span class=\"mtk8\">&quot;http://platform.linkedin.com/in.js&quot;</span><span class=\"mtk1\">&gt;</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">api_key: </span><span class=\"mtk12\">your_api_key_goes_here</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<p>You can find your API key inside your application, the first entry under OAuth Keys \"Consumer Key / API Key.\"  This script is used to load the LinkedIn script into your website, and it will not display anything on your page, at least not the frontend.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 768px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 43.69230769230769%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA7ElEQVQoz4VQy07DMBDM/39JD6CC4Ae49UChEqpUgUQjcohRYyd2bK/f7lJDUSlK57Ae2zv2zlT5B6D1KARjTI6jt+YPUkr5DFVZvPdCCACDRBsDPhwQnQ+FxhjTGb7FyDpK25ZwRldE3Dew7i0e50lUR6a1Zqwfut1jQ6+28rYeF0Qtqe/M1xNpWozGBs6tli+EzzbdzSu9ex8ePl0L8YIYXVlr0SHyN9JfP23nz/Wy3uXJ0X/FSilKafPRKCVzcMHoGDxeOecwyxI4AEgpsfkksGNsWDFYqTQYW7YHMcdarOEf/3g+nfBCzgV7IgEMx8Lgr7cAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"luciusblog2-2\"\n        title=\"luciusblog2-2\"\n        src=\"/static/35eb48de141166a71e21d580a6aaf5ce/e5715/luciusblog2-2.png\"\n        srcset=\"/static/35eb48de141166a71e21d580a6aaf5ce/a6d36/luciusblog2-2.png 650w,\n/static/35eb48de141166a71e21d580a6aaf5ce/e5715/luciusblog2-2.png 768w,\n/static/35eb48de141166a71e21d580a6aaf5ce/34e70/luciusblog2-2.png 1053w\"\n        sizes=\"(max-width: 768px) 100vw, 768px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>Next, you will need to add another script inside your body tag. It is actually used to display the sign-in button.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"js\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\"><span class=\"mtk1\">&lt;!--</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk12\">Hello</span><span class=\"mtk1\">, ; .</span></span>\n<span class=\"grvsc-line\"><span class=\"mtk1\">--&gt;</span></span></code></pre>\n<p>If everything has been implemented correctly, load your page, and you will see this lovely button appear on your site.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 143px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 19.58041958041958%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA2klEQVQI1z3I72uCQBzHcf/mQX9Bf8Ce92wDQ8hGLSJya0ZFRr8f1GS5lkPFO0/vLgd1MvXSe9D7Cy8+fKXRF5oe0CX555xnWZbnuUDIy4qdpqn4cUFZKpKq7f2DvN0f3d8fKwgjCFAAIfABxthzbOv76HoQRyGC/ulkez5wHNc0P8MwujImPXZ3leelsTb7r02l0ehp4/nMmOh64Yui9N8Hw6GxmRuz8aDZ7qiq2upocl1eL1ZXlki1t8PTh+UH+EwpAICQMy2uLL6LEMKExvEfpYSWIyaYMMZu7nLSNStwqCAAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"luciusblog2-3\"\n        title=\"luciusblog2-3\"\n        src=\"/static/9acfd927f44d45c20bb5213ecbb81e3f/a6b04/luciusblog2-3.png\"\n        srcset=\"/static/9acfd927f44d45c20bb5213ecbb81e3f/a6b04/luciusblog2-3.png 143w\"\n        sizes=\"(max-width: 143px) 100vw, 143px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span></p>\n<p>Want to extend your social systems with additional provider functionality? Check out this post on <a href=\"https://www.loginradius.com/blog/engineering/integrating-twitter-social-login/\" title=\"Integrating Twitter Social Login\">Twitter social login</a>.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n  .dark-default-dark .mtk1 { color: #D4D4D4; }\n  .dark-default-dark .mtk12 { color: #9CDCFE; }\n  .dark-default-dark .mtk8 { color: #CE9178; }\n</style>","frontmatter":{"title":"Integrating LinkedIn Social Login on a Website","author":{"id":"Lucius Yu","github":null,"avatar":null},"date":"February 11, 2015","updated_date":null,"tags":["LinkedIn","SocialLogin"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/a4e74ae24ffd6c107fe96587b9ac68eb/630fb/linkedin-feat-img.png","srcSet":"/static/a4e74ae24ffd6c107fe96587b9ac68eb/69585/linkedin-feat-img.png 200w,\n/static/a4e74ae24ffd6c107fe96587b9ac68eb/630fb/linkedin-feat-img.png 300w","sizes":"(max-width: 300px) 100vw, 300px"}}}},"fields":{"authorId":"Lucius Yu","slug":"/engineering/integrate-linkedin-social-login-website/"}}}]},"authorYaml":{"id":"Lucius Yu","bio":"Lucius is a Software Developer at LoginRadius. He was born and raised in China, but came to Edmonton to graduate from the University of Alberta, double majoring in Computer Sciences and Mathematics. Besides coding, which is his real passion, he enjoys playing ping pong and swimming.","github":null,"stackoverflow":null,"linkedin":null,"medium":null,"twitter":null,"avatar":null}},"pageContext":{"id":"Lucius Yu","__params":{"id":"lucius-yu"}}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}